Code for Advanced JMockit article (#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.
This commit is contained in:
Álvaro Fernández González 2016-07-31 22:05:45 +02:00 committed by Grzegorz Piwowarek
parent 135adf81b1
commit 944525a524
4 changed files with 188 additions and 0 deletions

View File

@ -6,3 +6,4 @@
### Relevant Articles:
- [JMockit 101](http://www.baeldung.com/jmockit-101)
- [A Guide to JMockit Expectations](http://www.baeldung.com/jmockit-expectations)
- [JMockit Advanced Topics](http://www.baeldung.com/jmockit-advanced-topics)

View File

@ -0,0 +1,20 @@
package org.baeldung.mocks.jmockit;
public class AdvancedCollaborator {
int i;
private int privateField = 5;
public AdvancedCollaborator(){}
public AdvancedCollaborator(String string) throws Exception{
i = string.length();
}
public String methodThatCallsPrivateMethod(int i){
return privateMethod() + i;
}
public int methodThatReturnsThePrivateField(){
return privateField;
}
private String privateMethod(){
return "default:";
}
class InnerAdvancedCollaborator{}
}

View File

@ -0,0 +1,57 @@
package org.baeldung.mocks.jmockit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import mockit.Tested;
import mockit.Verifications;
import mockit.integration.junit4.JMockit;
@RunWith(JMockit.class)
public class ReusingTest {
@Injectable
private Collaborator collaborator;
@Mocked
private Model model;
@Tested
private Performer performer;
@Before
public void setup(){
new Expectations(){{
model.getInfo(); result = "foo"; minTimes = 0;
collaborator.collaborate("foo"); result = true; minTimes = 0;
}};
}
@Test
public void testWithSetup() {
performer.perform(model);
verifyTrueCalls(1);
}
protected void verifyTrueCalls(int calls){
new Verifications(){{
collaborator.receive(true); times = calls;
}};
}
final class TrueCallsVerification extends Verifications{
public TrueCallsVerification(int calls){
collaborator.receive(true); times = calls;
}
}
@Test
public void testWithFinalClass() {
performer.perform(model);
new TrueCallsVerification(1);
}
}

View File

@ -0,0 +1,110 @@
package org.baeldung.mocks.jmockit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import org.baeldung.mocks.jmockit.AdvancedCollaborator.InnerAdvancedCollaborator;
import org.junit.Test;
import org.junit.runner.RunWith;
import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Invocation;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import mockit.Tested;
import mockit.integration.junit4.JMockit;
@RunWith(JMockit.class)
public class AdvancedCollaboratorTest<MultiMock extends List<String> & Comparable<List<String>>> {
@Tested
private AdvancedCollaborator mock;
@Mocked
private MultiMock multiMock;
@Test
public void testToMockUpPrivateMethod() {
new MockUp<AdvancedCollaborator>() {
@Mock
private String privateMethod() {
return "mocked: ";
}
};
String res = mock.methodThatCallsPrivateMethod(1);
assertEquals("mocked: 1", res);
}
@Test
public void testToMockUpDifficultConstructor() throws Exception {
new MockUp<AdvancedCollaborator>() {
@Mock
public void $init(Invocation invocation, String string) {
((AdvancedCollaborator) invocation.getInvokedInstance()).i = 1;
}
};
AdvancedCollaborator coll = new AdvancedCollaborator(null);
assertEquals(1, coll.i);
}
@Test
public void testToCallPrivateMethodsDirectly() {
Object value = Deencapsulation.invoke(mock, "privateMethod");
assertEquals("default:", value);
}
@Test
public void testToSetPrivateFieldDirectly() {
Deencapsulation.setField(mock, "privateField", 10);
assertEquals(10, mock.methodThatReturnsThePrivateField());
}
@Test
public void testToGetPrivateFieldDirectly() {
int value = Deencapsulation.getField(mock, "privateField");
assertEquals(5, value);
}
@Test
public void testToCreateNewInstanceDirectly() {
AdvancedCollaborator coll = Deencapsulation.newInstance(AdvancedCollaborator.class, "foo");
assertEquals(3, coll.i);
}
@Test
public void testToCreateNewInnerClassInstanceDirectly() {
InnerAdvancedCollaborator innerCollaborator = Deencapsulation.newInnerInstance(InnerAdvancedCollaborator.class, mock);
assertNotNull(innerCollaborator);
}
@Test
@SuppressWarnings("unchecked")
public void testMultipleInterfacesWholeTest() {
new Expectations() {
{
multiMock.get(5); result = "foo";
multiMock.compareTo((List<String>) any); result = 0;
}
};
assertEquals("foo", multiMock.get(5));
assertEquals(0, multiMock.compareTo(new ArrayList<>()));
}
@Test
@SuppressWarnings("unchecked")
public <M extends List<String> & Comparable<List<String>>> void testMultipleInterfacesOneMethod(@Mocked M mock) {
new Expectations() {
{
mock.get(5); result = "foo";
mock.compareTo((List<String>) any);
result = 0; }
};
assertEquals("foo", mock.get(5));
assertEquals(0, mock.compareTo(new ArrayList<>()));
}
}