Skip to content

Commit 34414b2

Browse files
author
Grzegorz Piwowarek
committed
Refactor JMockit examples
1 parent b9c4126 commit 34414b2

4 files changed

Lines changed: 90 additions & 104 deletions

File tree

‎mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.baeldung.mocks.jmockit;
22

33
public class Collaborator {
4+
45
public boolean collaborate(String string){
56
return false;
67
}
8+
79
public void receive(boolean bool){
810
//NOOP
911
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.baeldung.mocks.jmockit;
22

33
public class Model {
4-
public String getInfo(){
5-
return "info";
4+
public String getInfo() {
5+
return "info";
66
}
77
}
Lines changed: 84 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,124 @@
11
package org.baeldung.mocks.jmockit;
22

3-
import static org.junit.Assert.*;
4-
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
8-
import org.hamcrest.BaseMatcher;
9-
import org.hamcrest.Description;
10-
import org.junit.Test;
11-
import org.junit.runner.RunWith;
12-
133
import mockit.Delegate;
144
import mockit.Expectations;
155
import mockit.Mocked;
166
import mockit.StrictExpectations;
177
import mockit.Verifications;
188
import mockit.integration.junit4.JMockit;
9+
import org.hamcrest.BaseMatcher;
10+
import org.hamcrest.Description;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
import static org.junit.Assert.assertEquals;
1918

2019
@RunWith(JMockit.class)
2120
@SuppressWarnings("unchecked")
2221
public class ExpectationsTest {
2322

2423
@Test
2524
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
26-
new Expectations() {
27-
{
28-
mock.methodForAny1(anyString, anyInt, anyBoolean);
29-
result = "any";
30-
}
31-
};
25+
new Expectations() {{
26+
mock.methodForAny1(anyString, anyInt, anyBoolean);
27+
result = "any";
28+
}};
3229

3330
assertEquals("any", mock.methodForAny1("barfooxyz", 0, Boolean.FALSE));
3431
mock.methodForAny2(2L, new ArrayList<>());
3532

36-
new Verifications() {
37-
{
38-
mock.methodForAny2(anyLong, (List<String>) any);
39-
}
40-
};
33+
new Verifications() {{
34+
mock.methodForAny2(anyLong, (List<String>) any);
35+
}};
4136
}
4237

4338
@Test
4439
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
45-
new Expectations() {
46-
{
47-
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
48-
result = "with";
49-
}
50-
};
51-
40+
new Expectations() {{
41+
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
42+
result = "with";
43+
}};
44+
5245
assertEquals("with", mock.methodForWith1("barfooxyz", 2));
5346
mock.methodForWith2(Boolean.TRUE, new ArrayList<>());
54-
55-
new Verifications() {
56-
{
57-
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
58-
}
59-
};
47+
48+
new Verifications() {{
49+
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
50+
}};
6051
}
6152

6253
@Test
6354
public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
64-
new Expectations() {
65-
{
66-
mock.methodForNulls1(anyString, null);
67-
result = "null";
68-
}
69-
};
70-
55+
new Expectations() {{
56+
mock.methodForNulls1(anyString, null);
57+
result = "null";
58+
}};
59+
7160
assertEquals("null", mock.methodForNulls1("blablabla", new ArrayList<String>()));
7261
mock.methodForNulls2("blablabla", null);
73-
74-
new Verifications() {
75-
{
76-
mock.methodForNulls2(anyString, (List<String>) withNull());
77-
}
78-
};
62+
63+
new Verifications() {{
64+
mock.methodForNulls2(anyString, (List<String>) withNull());
65+
}};
7966
}
8067

8168
@Test
8269
public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
83-
new Expectations() {
84-
{
85-
mock.methodForTimes1(); times = 2;
86-
mock.methodForTimes2();
87-
}
88-
};
89-
70+
new Expectations() {{
71+
mock.methodForTimes1();
72+
times = 2;
73+
mock.methodForTimes2();
74+
}};
75+
9076
mock.methodForTimes1();
9177
mock.methodForTimes1();
9278
mock.methodForTimes2();
9379
mock.methodForTimes3();
9480
mock.methodForTimes3();
9581
mock.methodForTimes3();
96-
97-
new Verifications() {
98-
{
99-
mock.methodForTimes3(); minTimes = 1; maxTimes = 3;
100-
}
101-
};
82+
83+
new Verifications() {{
84+
mock.methodForTimes3();
85+
minTimes = 1;
86+
maxTimes = 3;
87+
}};
10288
}
10389

10490
@Test
10591
public void testCustomArgumentMatching(@Mocked ExpectationsCollaborator mock) {
106-
new Expectations() {
107-
{
108-
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
109-
@Override
110-
public boolean matches(Object item) {
111-
return item instanceof Model && "info".equals(((Model) item).getInfo());
112-
}
92+
new Expectations() {{
93+
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
94+
@Override
95+
public boolean matches(Object item) {
96+
return item instanceof Model && "info".equals(((Model) item).getInfo());
97+
}
11398

114-
@Override
115-
public void describeTo(Description description) { }
116-
}));
117-
}
118-
};
99+
@Override
100+
public void describeTo(Description description) {
101+
}
102+
}));
103+
}};
119104
mock.methodForArgThat(new Model());
120105
}
121106

122107
@Test
123108
public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
124-
new StrictExpectations() {
125-
{
126-
mock.methodReturnsString();
127-
result = "foo";
128-
result = new Exception();
129-
result = "bar";
130-
mock.methodReturnsInt();
131-
result = new int[] { 1, 2, 3 };
132-
mock.methodReturnsString();
133-
returns("foo", "bar");
134-
mock.methodReturnsInt();
135-
result = 1;
136-
}
137-
};
138-
109+
new StrictExpectations() {{
110+
mock.methodReturnsString();
111+
result = "foo";
112+
result = new Exception();
113+
result = "bar";
114+
mock.methodReturnsInt();
115+
result = new int[]{1, 2, 3};
116+
mock.methodReturnsString();
117+
returns("foo", "bar");
118+
mock.methodReturnsInt();
119+
result = 1;
120+
}};
121+
139122
assertEquals("Should return foo", "foo", mock.methodReturnsString());
140123
try {
141124
mock.methodReturnsString();
@@ -153,24 +136,23 @@ public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
153136

154137
@Test
155138
public void testDelegate(@Mocked ExpectationsCollaborator mock) {
156-
new Expectations() {
157-
{
158-
mock.methodForDelegate(anyInt);
159-
result = new Delegate() {
160-
public int delegate(int i) throws Exception {
161-
if (i < 3) {
162-
return 5;
163-
} else {
164-
throw new Exception();
165-
}
139+
new Expectations() {{
140+
mock.methodForDelegate(anyInt);
141+
result = new Delegate() {
142+
public int delegate(int i) throws Exception {
143+
if (i < 3) {
144+
return 5;
145+
} else {
146+
throw new Exception();
166147
}
167-
};
168-
}
169-
};
170-
148+
}
149+
};
150+
}};
151+
171152
assertEquals("Should return 5", 5, mock.methodForDelegate(1));
172153
try {
173154
mock.methodForDelegate(3);
174-
} catch (Exception e) { }
155+
} catch (Exception e) {
156+
}
175157
}
176158
}

‎mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public void testThePerformMethod(@Mocked Model model) {
2121
model.getInfo();result = "bar";
2222
collaborator.collaborate("bar"); result = true;
2323
}};
24+
2425
performer.perform(model);
26+
2527
new Verifications() {{
2628
collaborator.receive(true);
2729
}};

0 commit comments

Comments
 (0)