用Mockito繞過DAO層直接去測試Service層
這里用代碼的形式來記錄一下怎么使用Mockito,主要是怎么測試Private, static, 和 void 這種無返回值的方法。這里用到了另外一個包PowerMock。首先建一個父類BaseCar.java
1
package com.uv.smp.noah;
2
3
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17


18

19

20

21

22

23

再建一子類Ford.java
1
package com.uv.smp.noah;
2
3
import com.uv.smp.exception.AccessDeniedException;
4
import com.uv.smp.exception.DatabaseException;
5
import com.uv.smp.exception.EntityNotFoundException;
6
import com.uv.smp.exception.InvalidParameterException;
7
import com.uv.smp.exception.NotAuthenticatedException;
8
import com.uv.smp.model.provider.IProvider;
9
import com.uv.smp.persistence.IProviderHome;
10
11
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

接下來就是最重要的環節測試類FordTest.java
1
package com.uv.smp.noah;
2
3
import org.junit.Assert;
4
import org.junit.Test;
5
import org.junit.runner.RunWith;
6
import org.mockito.Mockito;
7
import org.powermock.api.mockito.PowerMockito;
8
import org.powermock.core.classloader.annotations.PrepareForTest;
9
import org.powermock.modules.junit4.PowerMockRunner;
10
11
import com.uv.smp.model.provider.IProvider;
12
import com.uv.smp.model.provider.impl.ProviderImpl;
13
14
@RunWith(PowerMockRunner.class)
15
@PrepareForTest({ Ford.class})
16
public 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的高級用法
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

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