Skip to content

Commit c738c9e

Browse files
varomorfpivovarit
authored andcommitted
Code for deepening on expectations (eugenp#511)
* Add new module for mocks comparison. * Add sources for testing. * Changes on testCase. * Enter some tests for mockito. * More tests for Mockito. * Even more tests. * Add the rest of the mocking libraries. * Javadoc on test. * Test bare bones for EasyMock. * Fist kind of test and setup. * Add tests using EasyMock with a change on LoginService. * Create LoginControllerTest.java * Test setup * [JMockit] No method called test. * [JMockit] Two methods called test. * [JMockit] One method called test. * [JMockit] Exception mock test * [JMockit] Mocked object to pass around test. * [JMockit] Custom matcher test. * [JMockit] Partial mocking test. * [JMockit] Fix with IDE. * Not stubs. Mocks. MOCKS!!! * Remove unnecesary import. * Use correct encoding. Was having problems with buildings. * Remove failing module. * Create new module mocks and move mock-comparisons there. * Add jmockit module. * Add model class. * Add collaborator class. * Add performer class. * Add performer test. * Fix * Add interface for tests. * Test for any. * Test for with. * Test for null. * Test for times. * Test for arg that. * Test for result and returns. * Test for delegate.
1 parent e883cf6 commit c738c9e

3 files changed

Lines changed: 176 additions & 3 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
import java.util.List;
4+
5+
public interface ExpectationsCollaborator {
6+
void methodForAny(String s, int i, Boolean b, List<String> l);
7+
void methodForWith(String s, int i, Boolean b, List<String> l);
8+
void methodForNulls(String s, List<String> l, List<Integer> m);
9+
void methodForTimes1();
10+
void methodForTimes2();
11+
void methodForTimes3();
12+
void methodForArgThat(Object o);
13+
String methodReturnsString();
14+
int methodReturnsInt();
15+
Object methodForDelegate(int i);
16+
}
Lines changed: 3 additions & 3 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";
6-
}
4+
public String getInfo(){
5+
return "info";
6+
}
77
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package org.baeldung.mocks.jmockit;
2+
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+
13+
import mockit.Delegate;
14+
import mockit.Expectations;
15+
import mockit.Mocked;
16+
import mockit.StrictExpectations;
17+
import mockit.integration.junit4.JMockit;
18+
19+
@RunWith(JMockit.class)
20+
@SuppressWarnings("unchecked")
21+
public class ExpectationsTest {
22+
23+
@Test
24+
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
25+
new Expectations() {
26+
{
27+
mock.methodForAny(anyString, anyInt, anyBoolean, (List<String>) any);
28+
}
29+
};
30+
mock.methodForAny("barfooxyz", 0, Boolean.FALSE, new ArrayList<>());
31+
}
32+
33+
@Test
34+
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
35+
new Expectations() {
36+
{
37+
mock.methodForWith(withSubstring("foo"), withNotEqual(1), withNotNull(), withInstanceOf(List.class));
38+
}
39+
};
40+
mock.methodForWith("barfooxyz", 2, Boolean.TRUE, new ArrayList<>());
41+
}
42+
43+
@Test
44+
public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
45+
// more config
46+
new Expectations() {
47+
{
48+
mock.methodForNulls(anyString, null, (List<Integer>) withNull());
49+
}
50+
};
51+
mock.methodForNulls("blablabla", new ArrayList<String>(), null);
52+
}
53+
54+
@Test
55+
public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
56+
// more config
57+
new Expectations() {
58+
{
59+
// exactly 2 invocations to foo() are expected
60+
mock.methodForTimes1();
61+
times = 2;
62+
// we expect from 1 to 3 invocations to bar()
63+
mock.methodForTimes2();
64+
minTimes = 1;
65+
maxTimes = 3;
66+
mock.methodForTimes3(); // "minTimes = 1" is implied
67+
}
68+
};
69+
mock.methodForTimes1();
70+
mock.methodForTimes1();
71+
mock.methodForTimes2();
72+
mock.methodForTimes2();
73+
mock.methodForTimes2();
74+
mock.methodForTimes3();
75+
}
76+
77+
@Test
78+
public void testCustomArgumentMatching(@Mocked ExpectationsCollaborator mock) {
79+
new Expectations() {
80+
{
81+
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
82+
@Override
83+
public boolean matches(Object item) {
84+
return item instanceof Model && "info".equals(((Model) item).getInfo());
85+
}
86+
87+
@Override
88+
public void describeTo(Description description) {
89+
// NOOP
90+
}
91+
}));
92+
}
93+
};
94+
mock.methodForArgThat(new Model());
95+
}
96+
97+
@Test
98+
public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
99+
new StrictExpectations() {
100+
{
101+
// return "foo", an exception and lastly "bar"
102+
mock.methodReturnsString();
103+
result = "foo";
104+
result = new Exception();
105+
result = "bar";
106+
// return 1, 2, 3
107+
mock.methodReturnsInt();
108+
result = new int[] { 1, 2, 3 };
109+
// return "foo" and "bar"
110+
mock.methodReturnsString();
111+
returns("foo", "bar");
112+
// return only 1
113+
mock.methodReturnsInt();
114+
result = 1;
115+
}
116+
};
117+
assertEquals("Should return foo", "foo", mock.methodReturnsString());
118+
try {
119+
mock.methodReturnsString();
120+
} catch (Exception e) {
121+
// NOOP
122+
}
123+
assertEquals("Should return bar", "bar", mock.methodReturnsString());
124+
assertEquals("Should return 1", 1, mock.methodReturnsInt());
125+
assertEquals("Should return 2", 2, mock.methodReturnsInt());
126+
assertEquals("Should return 3", 3, mock.methodReturnsInt());
127+
assertEquals("Should return foo", "foo", mock.methodReturnsString());
128+
assertEquals("Should return bar", "bar", mock.methodReturnsString());
129+
assertEquals("Should return 1", 1, mock.methodReturnsInt());
130+
}
131+
132+
@Test
133+
public void testDelegate(@Mocked ExpectationsCollaborator mock) {
134+
new StrictExpectations() {
135+
{
136+
// return "foo", an exception and lastly "bar"
137+
mock.methodForDelegate(anyInt);
138+
times = 2;
139+
result = new Delegate() {
140+
public int delegate(int i) throws Exception {
141+
if (i < 3) {
142+
return 5;
143+
} else {
144+
throw new Exception();
145+
}
146+
}
147+
};
148+
}
149+
};
150+
assertEquals("Should return 5", 5, mock.methodForDelegate(1));
151+
try {
152+
mock.methodForDelegate(3);
153+
} catch (Exception e) {
154+
// NOOP
155+
}
156+
}
157+
}

0 commit comments

Comments
 (0)