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