Skip to content

Commit 944525a

Browse files
varomorfpivovarit
authored andcommitted
Code for Advanced JMockit article (eugenp#557)
* 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. * Compress tests and fix one test. * Adding new article to readme. * [BAEL-178] Add collaborator for advanced article. * [BAEL-178] Add link to readme. * [BAEL-178] Add test for mockUp. * [BAEL-178] Add test for invoke method. * [BAEL-178] Add constructors and tests for mockup for constructors. * [BAEL-178] Add private fields and more test for deencapsulation. * [BAEL-178] Add inner class and test for instantiating inner classes. * [BAEL-178] Multimocks. * [BAEL-178] Add test for expectation reusing.
1 parent 135adf8 commit 944525a

4 files changed

Lines changed: 188 additions & 0 deletions

File tree

‎mocks/jmockit/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
### Relevant Articles:
77
- [JMockit 101](http://www.baeldung.com/jmockit-101)
88
- [A Guide to JMockit Expectations](http://www.baeldung.com/jmockit-expectations)
9+
- [JMockit Advanced Topics](http://www.baeldung.com/jmockit-advanced-topics)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
public class AdvancedCollaborator {
4+
int i;
5+
private int privateField = 5;
6+
public AdvancedCollaborator(){}
7+
public AdvancedCollaborator(String string) throws Exception{
8+
i = string.length();
9+
}
10+
public String methodThatCallsPrivateMethod(int i){
11+
return privateMethod() + i;
12+
}
13+
public int methodThatReturnsThePrivateField(){
14+
return privateField;
15+
}
16+
private String privateMethod(){
17+
return "default:";
18+
}
19+
class InnerAdvancedCollaborator{}
20+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
7+
import mockit.Expectations;
8+
import mockit.Injectable;
9+
import mockit.Mocked;
10+
import mockit.Tested;
11+
import mockit.Verifications;
12+
import mockit.integration.junit4.JMockit;
13+
14+
@RunWith(JMockit.class)
15+
public class ReusingTest {
16+
17+
@Injectable
18+
private Collaborator collaborator;
19+
20+
@Mocked
21+
private Model model;
22+
23+
@Tested
24+
private Performer performer;
25+
26+
@Before
27+
public void setup(){
28+
new Expectations(){{
29+
model.getInfo(); result = "foo"; minTimes = 0;
30+
collaborator.collaborate("foo"); result = true; minTimes = 0;
31+
}};
32+
}
33+
34+
@Test
35+
public void testWithSetup() {
36+
performer.perform(model);
37+
verifyTrueCalls(1);
38+
}
39+
40+
protected void verifyTrueCalls(int calls){
41+
new Verifications(){{
42+
collaborator.receive(true); times = calls;
43+
}};
44+
}
45+
46+
final class TrueCallsVerification extends Verifications{
47+
public TrueCallsVerification(int calls){
48+
collaborator.receive(true); times = calls;
49+
}
50+
}
51+
52+
@Test
53+
public void testWithFinalClass() {
54+
performer.perform(model);
55+
new TrueCallsVerification(1);
56+
}
57+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.baeldung.mocks.jmockit.AdvancedCollaborator.InnerAdvancedCollaborator;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
13+
import mockit.Deencapsulation;
14+
import mockit.Expectations;
15+
import mockit.Invocation;
16+
import mockit.Mock;
17+
import mockit.MockUp;
18+
import mockit.Mocked;
19+
import mockit.Tested;
20+
import mockit.integration.junit4.JMockit;
21+
22+
@RunWith(JMockit.class)
23+
public class AdvancedCollaboratorTest<MultiMock extends List<String> & Comparable<List<String>>> {
24+
25+
@Tested
26+
private AdvancedCollaborator mock;
27+
28+
@Mocked
29+
private MultiMock multiMock;
30+
31+
@Test
32+
public void testToMockUpPrivateMethod() {
33+
new MockUp<AdvancedCollaborator>() {
34+
@Mock
35+
private String privateMethod() {
36+
return "mocked: ";
37+
}
38+
};
39+
String res = mock.methodThatCallsPrivateMethod(1);
40+
assertEquals("mocked: 1", res);
41+
}
42+
43+
@Test
44+
public void testToMockUpDifficultConstructor() throws Exception {
45+
new MockUp<AdvancedCollaborator>() {
46+
@Mock
47+
public void $init(Invocation invocation, String string) {
48+
((AdvancedCollaborator) invocation.getInvokedInstance()).i = 1;
49+
}
50+
};
51+
AdvancedCollaborator coll = new AdvancedCollaborator(null);
52+
assertEquals(1, coll.i);
53+
}
54+
55+
@Test
56+
public void testToCallPrivateMethodsDirectly() {
57+
Object value = Deencapsulation.invoke(mock, "privateMethod");
58+
assertEquals("default:", value);
59+
}
60+
61+
@Test
62+
public void testToSetPrivateFieldDirectly() {
63+
Deencapsulation.setField(mock, "privateField", 10);
64+
assertEquals(10, mock.methodThatReturnsThePrivateField());
65+
}
66+
67+
@Test
68+
public void testToGetPrivateFieldDirectly() {
69+
int value = Deencapsulation.getField(mock, "privateField");
70+
assertEquals(5, value);
71+
}
72+
73+
@Test
74+
public void testToCreateNewInstanceDirectly() {
75+
AdvancedCollaborator coll = Deencapsulation.newInstance(AdvancedCollaborator.class, "foo");
76+
assertEquals(3, coll.i);
77+
}
78+
79+
@Test
80+
public void testToCreateNewInnerClassInstanceDirectly() {
81+
InnerAdvancedCollaborator innerCollaborator = Deencapsulation.newInnerInstance(InnerAdvancedCollaborator.class, mock);
82+
assertNotNull(innerCollaborator);
83+
}
84+
85+
@Test
86+
@SuppressWarnings("unchecked")
87+
public void testMultipleInterfacesWholeTest() {
88+
new Expectations() {
89+
{
90+
multiMock.get(5); result = "foo";
91+
multiMock.compareTo((List<String>) any); result = 0;
92+
}
93+
};
94+
assertEquals("foo", multiMock.get(5));
95+
assertEquals(0, multiMock.compareTo(new ArrayList<>()));
96+
}
97+
98+
@Test
99+
@SuppressWarnings("unchecked")
100+
public <M extends List<String> & Comparable<List<String>>> void testMultipleInterfacesOneMethod(@Mocked M mock) {
101+
new Expectations() {
102+
{
103+
mock.get(5); result = "foo";
104+
mock.compareTo((List<String>) any);
105+
result = 0; }
106+
};
107+
assertEquals("foo", mock.get(5));
108+
assertEquals(0, mock.compareTo(new ArrayList<>()));
109+
}
110+
}

0 commit comments

Comments
 (0)