Skip to content

Commit 11241ea

Browse files
varomorfpivovarit
authored andcommitted
Changes to code to add verifications (eugenp#518)
* 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. * Add verifications to any tests. * Add verifications to with test. * Add verification examples to methods using null. * Add verifications to methods using times. * Formatting.
1 parent 2f8dc9d commit 11241ea

2 files changed

Lines changed: 55 additions & 19 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import java.util.List;
44

55
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);
6+
String methodForAny1(String s, int i, Boolean b);
7+
void methodForAny2(Long l, List<String> lst);
8+
String methodForWith1(String s, int i);
9+
void methodForWith2(Boolean b, List<String> l);
10+
String methodForNulls1(String s, List<String> l);
11+
void methodForNulls2(String s, List<String> l);
912
void methodForTimes1();
1013
void methodForTimes2();
1114
void methodForTimes3();

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

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import mockit.Expectations;
1515
import mockit.Mocked;
1616
import mockit.StrictExpectations;
17+
import mockit.Verifications;
1718
import mockit.integration.junit4.JMockit;
1819

1920
@RunWith(JMockit.class)
@@ -24,54 +25,85 @@ public class ExpectationsTest {
2425
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
2526
new Expectations() {
2627
{
27-
mock.methodForAny(anyString, anyInt, anyBoolean, (List<String>) any);
28+
mock.methodForAny1(anyString, anyInt, anyBoolean);
29+
result = "any";
30+
}
31+
};
32+
33+
assertEquals("any", mock.methodForAny1("barfooxyz", 0, Boolean.FALSE));
34+
mock.methodForAny2(2L, new ArrayList<>());
35+
36+
new Verifications() {
37+
{
38+
mock.methodForAny2(anyLong, (List<String>) any);
2839
}
2940
};
30-
mock.methodForAny("barfooxyz", 0, Boolean.FALSE, new ArrayList<>());
3141
}
3242

3343
@Test
3444
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
3545
new Expectations() {
3646
{
37-
mock.methodForWith(withSubstring("foo"), withNotEqual(1), withNotNull(), withInstanceOf(List.class));
47+
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
48+
result = "with";
49+
}
50+
};
51+
52+
assertEquals("with", mock.methodForWith1("barfooxyz", 2));
53+
mock.methodForWith2(Boolean.TRUE, new ArrayList<>());
54+
55+
new Verifications() {
56+
{
57+
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
3858
}
3959
};
40-
mock.methodForWith("barfooxyz", 2, Boolean.TRUE, new ArrayList<>());
4160
}
4261

4362
@Test
4463
public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
45-
// more config
4664
new Expectations() {
4765
{
48-
mock.methodForNulls(anyString, null, (List<Integer>) withNull());
66+
mock.methodForNulls1(anyString, null);
67+
result = "null";
68+
}
69+
};
70+
71+
assertEquals("null", mock.methodForNulls1("blablabla", new ArrayList<String>()));
72+
mock.methodForNulls2("blablabla", null);
73+
74+
new Verifications() {
75+
{
76+
mock.methodForNulls2(anyString, (List<String>) withNull());
4977
}
5078
};
51-
mock.methodForNulls("blablabla", new ArrayList<String>(), null);
5279
}
5380

5481
@Test
5582
public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
56-
// more config
5783
new Expectations() {
5884
{
59-
// exactly 2 invocations to foo() are expected
85+
// exactly 2 invocations are expected
6086
mock.methodForTimes1();
6187
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
88+
mock.methodForTimes2(); // "minTimes = 1" is implied
6789
}
6890
};
91+
6992
mock.methodForTimes1();
7093
mock.methodForTimes1();
7194
mock.methodForTimes2();
72-
mock.methodForTimes2();
73-
mock.methodForTimes2();
7495
mock.methodForTimes3();
96+
mock.methodForTimes3();
97+
mock.methodForTimes3();
98+
99+
new Verifications() {
100+
{
101+
// we expect from 1 to 3 invocations
102+
mock.methodForTimes3();
103+
minTimes = 1;
104+
maxTimes = 3;
105+
}
106+
};
75107
}
76108

77109
@Test
@@ -114,6 +146,7 @@ public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
114146
result = 1;
115147
}
116148
};
149+
117150
assertEquals("Should return foo", "foo", mock.methodReturnsString());
118151
try {
119152
mock.methodReturnsString();

0 commit comments

Comments
 (0)