1
2
3
4
5
6 package otherTests;
7
8 import static org.hamcrest.CoreMatchers.is;
9 import static org.junit.jupiter.api.Assertions.assertEquals;
10 import static org.junit.jupiter.api.Assertions.assertFalse;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import mockit.ClassWithObjectOverrides;
18 import mockit.Delegate;
19 import mockit.Expectations;
20 import mockit.Injectable;
21 import mockit.Invocation;
22 import mockit.Mock;
23 import mockit.Mocked;
24 import mockit.Verifications;
25
26 import org.junit.Test;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33 public final class JUnit4Test {
34
35
36 private static final Logger logger = LoggerFactory.getLogger(JUnit4Test.class);
37
38
39 @Mocked
40 ClassWithObjectOverrides mock;
41
42
43
44
45 @SuppressWarnings("EqualsBetweenInconvertibleTypes")
46 @Test
47 public void useMockedInstance() {
48 new ClassWithObjectOverrides("test");
49 assertFalse(mock.toString().isEmpty());
50 mock.equals("123");
51
52 mock.equals(null);
53
54 new Verifications() {
55 {
56 String s;
57 mock.equals(s = withCapture());
58 assertEquals("123", s);
59
60 List<ClassWithObjectOverrides> objs = withCapture(new ClassWithObjectOverrides("test"));
61 assertEquals(1, objs.size());
62
63 mock.equals(withNull());
64 }
65 };
66 }
67
68
69
70
71 static class AnotherClass {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 void doSomething(int i, long l, Short s, byte b, char c, double d, float f, String str) {
94 logger.info("{}", i + l + s + b + d + f);
95 logger.info("{}", c + str);
96 }
97
98
99
100
101
102
103
104
105
106 String getModifiedValue(String s) {
107 return s.toLowerCase();
108 }
109 }
110
111
112
113
114
115
116
117 @Test
118 public void useArgumentMatchingFields(@Injectable final AnotherClass anotherMock) {
119 new Expectations() {
120 {
121 anotherMock.doSomething(anyInt, anyLong, anyShort, anyByte, anyChar, anyDouble, anyFloat, anyString);
122 }
123 };
124
125 anotherMock.doSomething(1, 2, (short) 3, (byte) 4, 'c', 1.2, 2.5F, "");
126 }
127
128
129
130
131
132
133
134 @Test
135 public void useArgumentMatchingMethods(@Injectable final AnotherClass anotherMock) {
136 new Expectations() {
137 {
138 anotherMock.doSomething(withAny(0), withEqual(2L), withInstanceOf(short.class), withNotEqual((byte) 1),
139 withInstanceLike(' '), withEqual(1.2, 0), withEqual(2.5F, 0), withSameInstance("test"));
140
141 anotherMock.getModifiedValue(withSubstring("abc"));
142 result = "Abc";
143 anotherMock.getModifiedValue(withPrefix("TX"));
144 result = "abc";
145 anotherMock.getModifiedValue(withSuffix("X"));
146 result = "ABC";
147 anotherMock.getModifiedValue(withMatch("\\d+"));
148 result = "number";
149 anotherMock.getModifiedValue(withArgThat(is("test")));
150 result = "test";
151
152 anotherMock.getModifiedValue("Delegate");
153 result = new Delegate<Object>() {
154 @Mock
155 String delegate(Invocation inv, String s) {
156 assertNotNull(inv.getInvokedMember());
157 assertTrue(inv.getInvocationIndex() >= 0);
158 assertTrue(inv.getInvocationCount() >= 1);
159 assertEquals(1, inv.getInvokedArguments().length);
160 return inv.proceed();
161 }
162 };
163 }
164 };
165
166 anotherMock.doSomething(1, 2, (short) 3, (byte) 4, 'c', 1.2, 2.5F, "test");
167
168 assertEquals("Abc", anotherMock.getModifiedValue("test abc xyz"));
169 assertEquals("abc", anotherMock.getModifiedValue("TX test"));
170 assertEquals("ABC", anotherMock.getModifiedValue("test X"));
171 assertEquals("number", anotherMock.getModifiedValue("123"));
172 assertEquals("test", anotherMock.getModifiedValue("test"));
173 assertEquals("delegate", anotherMock.getModifiedValue("Delegate"));
174
175 new Verifications() {
176 {
177 List<String> values = new ArrayList<>();
178 anotherMock.getModifiedValue(withCapture(values));
179 assertEquals(6, values.size());
180 }
181 };
182 }
183 }