<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    用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 眼鏡蛇 閱讀(6457) 評論(0)  編輯  收藏 所屬分類: Mockito


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


    網站導航:
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久亚洲欧洲国产综合| 亚洲精品中文字幕乱码| 亚洲人精品午夜射精日韩| 久久久亚洲欧洲日产国码aⅴ| 亚洲五月综合网色九月色| 亚洲一区二区三区免费| 国产无人区码卡二卡三卡免费| 中文字幕第13亚洲另类| 一级毛片在线免费播放| 老司机在线免费视频| 亚洲国产日韩在线人成下载| 美女被免费网站91色| 午夜a级成人免费毛片| 亚洲AV美女一区二区三区| 黄色大片免费网站| 野花高清在线观看免费3中文 | 亚洲色大成网站www尤物| 最近免费中文字幕中文高清 | 又粗又黄又猛又爽大片免费| 亚洲精品无码久久久久久久| 精品国产sm捆绑最大网免费站| 亚洲免费视频一区二区三区| 亚洲一本到无码av中文字幕| 国产精品自在自线免费观看| 亚洲成av人片在线看片| 暖暖免费在线中文日本| 四虎国产精品免费视| 亚洲精品乱码久久久久久蜜桃图片| 91人成网站色www免费下载| 亚洲一区无码精品色| 国产偷国产偷亚洲高清人| 好吊妞在线新免费视频| 亚洲一卡一卡二新区无人区| 又粗又黄又猛又爽大片免费| 好紧我太爽了视频免费国产| 亚洲中文字幕在线第六区| 国产特黄特色的大片观看免费视频| 亚洲国产综合专区电影在线| 麻豆精品成人免费国产片| 中文字幕在线日亚洲9| 拨牐拨牐x8免费|