Merge remote-tracking branch 'upstream/master'
This commit is contained in:
Executable
+20
@@ -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{}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Collaborator {
|
||||
|
||||
public boolean collaborate(String string){
|
||||
return false;
|
||||
}
|
||||
|
||||
public void receive(boolean bool){
|
||||
//NOOP
|
||||
}
|
||||
}
|
||||
testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/ExpectationsCollaborator.java
Executable
+19
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ExpectationsCollaborator {
|
||||
String methodForAny1(String s, int i, Boolean b);
|
||||
void methodForAny2(Long l, List<String> lst);
|
||||
String methodForWith1(String s, int i);
|
||||
void methodForWith2(Boolean b, List<String> l);
|
||||
String methodForNulls1(String s, List<String> l);
|
||||
void methodForNulls2(String s, List<String> l);
|
||||
void methodForTimes1();
|
||||
void methodForTimes2();
|
||||
void methodForTimes3();
|
||||
void methodForArgThat(Object o);
|
||||
String methodReturnsString();
|
||||
int methodReturnsInt();
|
||||
Object methodForDelegate(int i);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Model {
|
||||
public String getInfo() {
|
||||
return "info";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Performer {
|
||||
private Collaborator collaborator;
|
||||
|
||||
public void perform(Model model){
|
||||
boolean value = collaborator.collaborate(model.getInfo());
|
||||
collaborator.receive(value);
|
||||
}
|
||||
}
|
||||
+110
@@ -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 AdvancedCollaboratorIntegrationTest<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<>()));
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
import mockit.Delegate;
|
||||
import mockit.Expectations;
|
||||
import mockit.Mocked;
|
||||
import mockit.StrictExpectations;
|
||||
import mockit.Verifications;
|
||||
import mockit.integration.junit4.JMockit;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(JMockit.class)
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ExpectationsIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
|
||||
new Expectations() {{
|
||||
mock.methodForAny1(anyString, anyInt, anyBoolean);
|
||||
result = "any";
|
||||
}};
|
||||
|
||||
assertEquals("any", mock.methodForAny1("barfooxyz", 0, Boolean.FALSE));
|
||||
mock.methodForAny2(2L, new ArrayList<>());
|
||||
|
||||
new Verifications() {{
|
||||
mock.methodForAny2(anyLong, (List<String>) any);
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
|
||||
new Expectations() {{
|
||||
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
|
||||
result = "with";
|
||||
}};
|
||||
|
||||
assertEquals("with", mock.methodForWith1("barfooxyz", 2));
|
||||
mock.methodForWith2(Boolean.TRUE, new ArrayList<>());
|
||||
|
||||
new Verifications() {{
|
||||
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
|
||||
new Expectations() {{
|
||||
mock.methodForNulls1(anyString, null);
|
||||
result = "null";
|
||||
}};
|
||||
|
||||
assertEquals("null", mock.methodForNulls1("blablabla", new ArrayList<String>()));
|
||||
mock.methodForNulls2("blablabla", null);
|
||||
|
||||
new Verifications() {{
|
||||
mock.methodForNulls2(anyString, (List<String>) withNull());
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
|
||||
new Expectations() {{
|
||||
mock.methodForTimes1();
|
||||
times = 2;
|
||||
mock.methodForTimes2();
|
||||
}};
|
||||
|
||||
mock.methodForTimes1();
|
||||
mock.methodForTimes1();
|
||||
mock.methodForTimes2();
|
||||
mock.methodForTimes3();
|
||||
mock.methodForTimes3();
|
||||
mock.methodForTimes3();
|
||||
|
||||
new Verifications() {{
|
||||
mock.methodForTimes3();
|
||||
minTimes = 1;
|
||||
maxTimes = 3;
|
||||
}};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomArgumentMatching(@Mocked ExpectationsCollaborator mock) {
|
||||
new Expectations() {{
|
||||
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
|
||||
@Override
|
||||
public boolean matches(Object item) {
|
||||
return item instanceof Model && "info".equals(((Model) item).getInfo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
}
|
||||
}));
|
||||
}};
|
||||
mock.methodForArgThat(new Model());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
|
||||
new StrictExpectations() {{
|
||||
mock.methodReturnsString();
|
||||
result = "foo";
|
||||
result = new Exception();
|
||||
result = "bar";
|
||||
mock.methodReturnsInt();
|
||||
result = new int[]{1, 2, 3};
|
||||
mock.methodReturnsString();
|
||||
returns("foo", "bar");
|
||||
mock.methodReturnsInt();
|
||||
result = 1;
|
||||
}};
|
||||
|
||||
assertEquals("Should return foo", "foo", mock.methodReturnsString());
|
||||
try {
|
||||
mock.methodReturnsString();
|
||||
} catch (Exception e) {
|
||||
// NOOP
|
||||
}
|
||||
assertEquals("Should return bar", "bar", mock.methodReturnsString());
|
||||
assertEquals("Should return 1", 1, mock.methodReturnsInt());
|
||||
assertEquals("Should return 2", 2, mock.methodReturnsInt());
|
||||
assertEquals("Should return 3", 3, mock.methodReturnsInt());
|
||||
assertEquals("Should return foo", "foo", mock.methodReturnsString());
|
||||
assertEquals("Should return bar", "bar", mock.methodReturnsString());
|
||||
assertEquals("Should return 1", 1, mock.methodReturnsInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelegate(@Mocked ExpectationsCollaborator mock) {
|
||||
new Expectations() {{
|
||||
mock.methodForDelegate(anyInt);
|
||||
result = new Delegate() {
|
||||
public int delegate(int i) throws Exception {
|
||||
if (i < 3) {
|
||||
return 5;
|
||||
} else {
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
};
|
||||
}};
|
||||
|
||||
assertEquals("Should return 5", 5, mock.methodForDelegate(1));
|
||||
try {
|
||||
mock.methodForDelegate(3);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerIntegrationTest.java
Executable
+32
@@ -0,0 +1,32 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import mockit.*;
|
||||
import mockit.integration.junit4.JMockit;
|
||||
|
||||
@RunWith(JMockit.class)
|
||||
public class PerformerIntegrationTest {
|
||||
|
||||
@Injectable
|
||||
private Collaborator collaborator;
|
||||
|
||||
@Tested
|
||||
private Performer performer;
|
||||
|
||||
@Test
|
||||
public void testThePerformMethod(@Mocked Model model) {
|
||||
new Expectations() {{
|
||||
model.getInfo();result = "bar";
|
||||
collaborator.collaborate("bar"); result = true;
|
||||
}};
|
||||
|
||||
performer.perform(model);
|
||||
|
||||
new Verifications() {{
|
||||
collaborator.receive(true);
|
||||
}};
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+57
@@ -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 ReusingIntegrationTest {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user