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


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


    網站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久国产免费直播| 98精品全国免费观看视频| 国产精品亚洲二区在线观看| 99在线免费视频| 亚洲另类春色校园小说| 国产成人高清精品免费软件| 久久国产一片免费观看| 国产v亚洲v天堂a无| 亚洲AV伊人久久青青草原| 无码囯产精品一区二区免费| 亚洲成av人片在www鸭子| 亚洲乱亚洲乱妇无码麻豆| 国语成本人片免费av无码| jizz免费观看| 亚洲六月丁香婷婷综合| 久久久久亚洲AV成人网人人网站| 美女内射毛片在线看免费人动物 | 亚洲精品无码专区在线| 久久99国产亚洲高清观看首页| 91精品免费在线观看| 成人免费av一区二区三区| 亚洲色偷偷综合亚洲AV伊人蜜桃 | 久久精品国产亚洲网站| 午夜高清免费在线观看| 99免费在线观看视频| 一级毛片在线播放免费| 亚洲熟女精品中文字幕| 亚洲邪恶天堂影院在线观看| 日韩亚洲精品福利| 日韩吃奶摸下AA片免费观看| 免费视频成人手机在线观看网址| 在线观看亚洲免费视频| 亚洲AV无码久久久久网站蜜桃 | 国产精品亚洲高清一区二区| 男人的好看免费观看在线视频 | 99精品视频免费在线观看| 国产成人1024精品免费| 激情小说亚洲色图| 亚洲黄页网在线观看| 亚洲视频免费观看| 亚洲激情中文字幕|