用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 眼镜蛇 阅读(6400) 评论(0)  编辑  收藏 所属分类: Mockito


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜