用Mockito繞過DAO層直接去測試Service層

          這里用代碼的形式來記錄一下怎么使用Mockito,主要是怎么測試Private, static, 和 void 這種無返回值的方法。這里用到了另外一個包PowerMock。

          首先建一個父類BaseCar.java
           1package com.uv.smp.noah;
           2
           3public class BaseCar {
           4
           5    protected String getType(String type){
           6        return type;
           7    }

           8    
           9    public static int getWheelNum(){
          10        return 4;
          11    }

          12    
          13    public void checkSafetyBelt(){
          14        System.out.println("safety.");
          15    }

          16    
          17    public boolean hasAnyPermission(String a, Long longs){
          18        System.out.println("a= "+ a);
          19        System.out.println("Long= "+ longs);
          20        return false;
          21    }

          22}

          23

          再建一子類Ford.java
          1package com.uv.smp.noah;
          2
          3import com.uv.smp.exception.AccessDeniedException;
          4import com.uv.smp.exception.DatabaseException;
          5import com.uv.smp.exception.EntityNotFoundException;
          6import com.uv.smp.exception.InvalidParameterException;
          7import com.uv.smp.exception.NotAuthenticatedException;
          8import com.uv.smp.model.provider.IProvider;
          9import com.uv.smp.persistence.IProviderHome;
          10
          11public class Ford extends BaseCar {
          12
          13 private IProviderHome providerHome;
          14
          15 final private int private_method(int a) {
          16 return a;
          17 }

          18
          19 public int test_private_method(int a) {
          20 return private_method(a);
          21 }

          22
          23 public static int static_return_method() {
          24 return 1;
          25 }

          26
          27 void void_method(int a) {
          28 System.out.println(a);
          29 throw new IllegalStateException("should not go here");
          30 }

          31 private void void_private_method() {
          32 throw new IllegalStateException("should not go here");
          33 }

          34
          35 public static void static_void_method() {
          36 throw new IllegalStateException("should not go here");
          37 }

          38
          39 public static void staticMethod(String a) {
          40 throw new IllegalStateException(a);
          41 }

          42
          43 public boolean callAnotherMethod(IProvider provider, int a) throws Exception{
          44 int aa = test_private_method(a);
          45 System.out.println("1>>>>>>>> "+ aa);
          46
          47 aa = static_return_method();
          48 System.out.println("2>>>>>>>> "+ aa);
          49
          50 void_private_method();
          51 System.out.println("3>>>>>>>> "+ aa);
          52 if(!hasAnyPermission("aaa", 123l,234l)){
          53
          54 System.out.println("4>>>>>>>> "+ hasAnyPermission("aaa", 123l,234l));
          55 throw new Exception();
          56 }

          57
          58 IProvider out = providerHome.saveProvider(provider);
          59 return true;
          60 }

          61
          62 public boolean callAnotherWithLooooooooooooooooooooooooooooooooooooooooooooooooooooooongMethod(int a) throws DatabaseException, AccessDeniedException, InvalidParameterException, EntityNotFoundException, NotAuthenticatedException{
          63 int aa = test_private_method(a);
          64 System.out.println("1>>>>>>>> "+ aa);
          65
          66 aa = static_return_method();
          67 System.out.println("2>>>>>>>> "+ aa);
          68
          69 void_private_method();
          70 System.out.println("3>>>>>>>> "+ aa);
          71 return true;
          72 }

          73}

          74

          接下來就是最重要的環節測試類FordTest.java


           
          1package com.uv.smp.noah;
          2
          3import org.junit.Assert;
          4import org.junit.Test;
          5import org.junit.runner.RunWith;
          6import org.mockito.Mockito;
          7import org.powermock.api.mockito.PowerMockito;
          8import org.powermock.core.classloader.annotations.PrepareForTest;
          9import org.powermock.modules.junit4.PowerMockRunner;
          10
          11import com.uv.smp.model.provider.IProvider;
          12import com.uv.smp.model.provider.impl.ProviderImpl;
          13
          14@RunWith(PowerMockRunner.class)
          15@PrepareForTest({ Ford.class})
          16public class FordTest {
          17 @Test
          18 public void testPrivateMethod() throws Exception {
          19 Ford spy = PowerMockito.spy(new Ford());
          20 PowerMockito.doReturn(3).when(spy, "private_method", 1);
          21 Assert.assertEquals(3, spy.test_private_method(1));
          22 PowerMockito.verifyPrivate(spy, Mockito.times(1)).invoke("private_method", 1);
          23 }

          24
          25 @Test
          26 public void testStaticReturnMethod() throws Exception {
          27
          28 PowerMockito.mockStatic(Ford.class);
          29 Mockito.when(Ford.static_return_method()).thenReturn(2);
          30 Assert.assertEquals(2, Ford.static_return_method());
          31 }

          32
          33 @Test
          34 public void testVoidMethod() throws Exception {
          35
          36 Ford spy = PowerMockito.spy(new Ford());
          37 PowerMockito.doNothing().when(spy, "void_method", 2);
          38 spy.void_method(2);
          39 }

          40 @Test
          41 public void testVoidPrivateMethod() throws Exception {
          42
          43 Ford spy = PowerMockito.spy(new Ford());
          44 PowerMockito.doNothing().when(spy, "void_private_method");
          45 PowerMockito.verifyPrivate(spy, Mockito.times(0)).invoke("void_private_method");
          46 }

          47
          48 @Test
          49 public void testStaticMethod1() throws Exception {
          50
          51 PowerMockito.mockStatic(Ford.class);
          52 PowerMockito.doNothing().when(Ford.class, "static_void_method");
          53 Ford.static_void_method();
          54 }

          55
          56 @Test
          57 public void testStaticMethod2() throws Exception {
          58
          59 PowerMockito.mockStatic(Ford.class);
          60 PowerMockito.doNothing().when(Ford.class, "staticMethod", "123");
          61 Ford.staticMethod("123");
          62
          63 PowerMockito.doNothing().when(Ford.class, "staticMethod", Mockito.anyString());
          64 Ford.staticMethod("456");
          65 }

          66
          67 @Test
          68 public void testCallAnotherMethod() throws Exception {
          69 PowerMockito.mockStatic(Ford.class);
          70 //IProviderHome providerHome = PowerMockito.mock(IProviderHome.class);
          71 Ford spy = PowerMockito.spy(new Ford());
          72 PowerMockito.doNothing().when(Ford.class, "static_void_method");
          73 PowerMockito.doNothing().when(spy, "void_private_method");
          74 PowerMockito.when(spy.hasAnyPermission("aaa", 123l,234l)).thenReturn(true);
          75
          76 IProvider provider = new ProviderImpl();
          77 IProvider newProvider = new ProviderImpl();
          78 newProvider.setId(123l);
          79 newProvider.setVerified(false);
          80
          81 //PowerMockito.when(providerHome.saveProvider(provider)).thenReturn(newProvider);
          82
          83
          84 Assert.assertEquals(true, spy.callAnotherMethod(provider, 3));
          85 }

          86 @Test
          87 public void testCallAnotherWithLongMethod() throws Exception {
          88 PowerMockito.mockStatic(Ford.class);
          89 Ford spy = PowerMockito.spy(new Ford());
          90 Mockito.when(Ford.static_return_method()).thenReturn(2);
          91 PowerMockito.doNothing().when(spy, "void_private_method");
          92
          93 Assert.assertEquals(true, spy.callAnotherWithLooooooooooooooooooooooooooooooooooooooooooooooooooooooongMethod(3));
          94 }

          95
          96 @Test
          97 public void testSupperMethod(){
          98 Ford spy = PowerMockito.spy(new Ford());
          99 PowerMockito.when(spy.getType("a")).thenReturn("Noah");
          100 Assert.assertEquals("Noah", spy.getType("a"));
          101 }

          102
          103
          104 @Test
          105 public void testSupperMethods(){
          106 Ford spy = PowerMockito.spy(new Ford());
          107 PowerMockito.when(spy.hasAnyPermission("aaa", 123l,234l)).thenReturn(true);
          108 Assert.assertEquals(true, spy.hasAnyPermission("aaa", 123l,234l));
          109 }

          110
          111 @Test
          112 public void testSupperStaticMethod(){
          113 PowerMockito.mockStatic(BaseCar.class);// could't use Ford.class
          114 Ford spy = PowerMockito.spy(new Ford());
          115 PowerMockito.when(Ford.getWheelNum()).thenReturn(2);
          116 Assert.assertEquals(2, spy.getWheelNum());
          117 }

          118}

          119
          Mockito的高級用法

          Mockito的高級用法

          Mockito的高級用法



          眼鏡蛇

          posted on 2013-10-10 18:00 眼鏡蛇 閱讀(6449) 評論(0)  編輯  收藏 所屬分類: Mockito


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 资溪县| 原阳县| 黄浦区| 平陆县| 犍为县| 仙游县| 拜泉县| 那曲县| 疏附县| 义乌市| 余庆县| 巴林右旗| 泰州市| 金乡县| 六盘水市| 大荔县| 新晃| 三河市| 右玉县| 前郭尔| 曲靖市| 永城市| 郓城县| 城市| 富源县| 句容市| 白沙| 远安县| 长乐市| 龙陵县| 连城县| 洱源县| 武平县| 青神县| 东光县| 和龙市| 遂平县| 平舆县| 兴文县| 临沧市| 唐山市|