1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertFalse;
9 import static org.junit.jupiter.api.Assertions.assertNotNull;
10 import static org.junit.jupiter.api.Assertions.assertNull;
11 import static org.junit.jupiter.api.Assertions.assertSame;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import mockit.integration.junit5.JMockitExtension;
15
16 import org.junit.jupiter.api.BeforeEach;
17 import org.junit.jupiter.api.Test;
18 import org.junit.jupiter.api.extension.ExtendWith;
19
20
21
22
23 @ExtendWith(JMockitExtension.class)
24 class TestedClassWithNoDITest {
25
26
27
28
29 public static final class TestedClass {
30
31
32 private final Dependency dependency = new Dependency();
33
34
35
36
37
38
39 public boolean doSomeOperation() {
40 return dependency.doSomething() > 0;
41 }
42 }
43
44
45
46
47 static class Dependency {
48
49
50
51
52
53 int doSomething() {
54 return -1;
55 }
56 }
57
58
59 @Tested
60 TestedClass tested1;
61
62
63 @Tested
64 final TestedClass tested2 = new TestedClass();
65
66
67 @Tested
68 TestedClass tested3;
69
70
71 @Tested
72 NonPublicTestedClass tested4;
73
74
75 @Tested
76 final TestedClass tested5 = null;
77
78
79 @Mocked
80 Dependency mock;
81
82
83 TestedClass tested;
84
85
86
87
88 @BeforeEach
89 void setUp() {
90 assertNotNull(mock);
91 assertNull(tested);
92 tested = new TestedClass();
93 assertNull(tested3);
94 tested3 = tested;
95 assertNull(tested1);
96 assertNotNull(tested2);
97 assertNull(tested4);
98 assertNull(tested5);
99 }
100
101
102
103
104 @Test
105 void verifyTestedFields() {
106 assertNull(tested5);
107 assertNotNull(tested4);
108 assertNotNull(tested3);
109 assertSame(tested, tested3);
110 assertNotNull(tested2);
111 assertNotNull(tested1);
112 }
113
114
115
116
117 @Test
118 void exerciseAutomaticallyInstantiatedTestedObject() {
119 new Expectations() {
120 {
121 mock.doSomething();
122 result = 1;
123 }
124 };
125
126 assertTrue(tested1.doSomeOperation());
127 }
128
129
130
131
132 @Test
133 void exerciseManuallyInstantiatedTestedObject() {
134 new Expectations() {
135 {
136 mock.doSomething();
137 result = 1;
138 }
139 };
140
141 assertTrue(tested2.doSomeOperation());
142
143 new FullVerifications() {
144 };
145 }
146
147
148
149
150 @Test
151 void exerciseAnotherManuallyInstantiatedTestedObject() {
152 assertFalse(tested3.doSomeOperation());
153
154 new Verifications() {
155 {
156 mock.doSomething();
157 times = 1;
158 }
159 };
160 }
161 }
162
163 class NonPublicTestedClass {
164 @SuppressWarnings("RedundantNoArgConstructor")
165 NonPublicTestedClass() {
166 }
167 }