Refactor JMockit examples
This commit is contained in:
parent
b9c412689d
commit
34414b2a43
|
@ -1,9 +1,11 @@
|
||||||
package org.baeldung.mocks.jmockit;
|
package org.baeldung.mocks.jmockit;
|
||||||
|
|
||||||
public class Collaborator {
|
public class Collaborator {
|
||||||
|
|
||||||
public boolean collaborate(String string){
|
public boolean collaborate(String string){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receive(boolean bool){
|
public void receive(boolean bool){
|
||||||
//NOOP
|
//NOOP
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package org.baeldung.mocks.jmockit;
|
package org.baeldung.mocks.jmockit;
|
||||||
|
|
||||||
public class Model {
|
public class Model {
|
||||||
public String getInfo(){
|
public String getInfo() {
|
||||||
return "info";
|
return "info";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,20 @@
|
||||||
package org.baeldung.mocks.jmockit;
|
package org.baeldung.mocks.jmockit;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.hamcrest.BaseMatcher;
|
|
||||||
import org.hamcrest.Description;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import mockit.Delegate;
|
import mockit.Delegate;
|
||||||
import mockit.Expectations;
|
import mockit.Expectations;
|
||||||
import mockit.Mocked;
|
import mockit.Mocked;
|
||||||
import mockit.StrictExpectations;
|
import mockit.StrictExpectations;
|
||||||
import mockit.Verifications;
|
import mockit.Verifications;
|
||||||
import mockit.integration.junit4.JMockit;
|
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)
|
@RunWith(JMockit.class)
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
@ -23,69 +22,56 @@ public class ExpectationsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
|
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
|
||||||
new Expectations() {
|
new Expectations() {{
|
||||||
{
|
mock.methodForAny1(anyString, anyInt, anyBoolean);
|
||||||
mock.methodForAny1(anyString, anyInt, anyBoolean);
|
result = "any";
|
||||||
result = "any";
|
}};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
assertEquals("any", mock.methodForAny1("barfooxyz", 0, Boolean.FALSE));
|
assertEquals("any", mock.methodForAny1("barfooxyz", 0, Boolean.FALSE));
|
||||||
mock.methodForAny2(2L, new ArrayList<>());
|
mock.methodForAny2(2L, new ArrayList<>());
|
||||||
|
|
||||||
new Verifications() {
|
new Verifications() {{
|
||||||
{
|
mock.methodForAny2(anyLong, (List<String>) any);
|
||||||
mock.methodForAny2(anyLong, (List<String>) any);
|
}};
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
|
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
|
||||||
new Expectations() {
|
new Expectations() {{
|
||||||
{
|
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
|
||||||
mock.methodForWith1(withSubstring("foo"), withNotEqual(1));
|
result = "with";
|
||||||
result = "with";
|
}};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
assertEquals("with", mock.methodForWith1("barfooxyz", 2));
|
assertEquals("with", mock.methodForWith1("barfooxyz", 2));
|
||||||
mock.methodForWith2(Boolean.TRUE, new ArrayList<>());
|
mock.methodForWith2(Boolean.TRUE, new ArrayList<>());
|
||||||
|
|
||||||
new Verifications() {
|
new Verifications() {{
|
||||||
{
|
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
|
||||||
mock.methodForWith2(withNotNull(), withInstanceOf(List.class));
|
}};
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
|
public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
|
||||||
new Expectations() {
|
new Expectations() {{
|
||||||
{
|
mock.methodForNulls1(anyString, null);
|
||||||
mock.methodForNulls1(anyString, null);
|
result = "null";
|
||||||
result = "null";
|
}};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
assertEquals("null", mock.methodForNulls1("blablabla", new ArrayList<String>()));
|
assertEquals("null", mock.methodForNulls1("blablabla", new ArrayList<String>()));
|
||||||
mock.methodForNulls2("blablabla", null);
|
mock.methodForNulls2("blablabla", null);
|
||||||
|
|
||||||
new Verifications() {
|
new Verifications() {{
|
||||||
{
|
mock.methodForNulls2(anyString, (List<String>) withNull());
|
||||||
mock.methodForNulls2(anyString, (List<String>) withNull());
|
}};
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
|
public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
|
||||||
new Expectations() {
|
new Expectations() {{
|
||||||
{
|
mock.methodForTimes1();
|
||||||
mock.methodForTimes1(); times = 2;
|
times = 2;
|
||||||
mock.methodForTimes2();
|
mock.methodForTimes2();
|
||||||
}
|
}};
|
||||||
};
|
|
||||||
|
|
||||||
mock.methodForTimes1();
|
mock.methodForTimes1();
|
||||||
mock.methodForTimes1();
|
mock.methodForTimes1();
|
||||||
|
@ -94,47 +80,44 @@ public class ExpectationsTest {
|
||||||
mock.methodForTimes3();
|
mock.methodForTimes3();
|
||||||
mock.methodForTimes3();
|
mock.methodForTimes3();
|
||||||
|
|
||||||
new Verifications() {
|
new Verifications() {{
|
||||||
{
|
mock.methodForTimes3();
|
||||||
mock.methodForTimes3(); minTimes = 1; maxTimes = 3;
|
minTimes = 1;
|
||||||
}
|
maxTimes = 3;
|
||||||
};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCustomArgumentMatching(@Mocked ExpectationsCollaborator mock) {
|
public void testCustomArgumentMatching(@Mocked ExpectationsCollaborator mock) {
|
||||||
new Expectations() {
|
new Expectations() {{
|
||||||
{
|
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
|
||||||
mock.methodForArgThat(withArgThat(new BaseMatcher<Object>() {
|
@Override
|
||||||
@Override
|
public boolean matches(Object item) {
|
||||||
public boolean matches(Object item) {
|
return item instanceof Model && "info".equals(((Model) item).getInfo());
|
||||||
return item instanceof Model && "info".equals(((Model) item).getInfo());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void describeTo(Description description) { }
|
public void describeTo(Description description) {
|
||||||
}));
|
}
|
||||||
}
|
}));
|
||||||
};
|
}};
|
||||||
mock.methodForArgThat(new Model());
|
mock.methodForArgThat(new Model());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
|
public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
|
||||||
new StrictExpectations() {
|
new StrictExpectations() {{
|
||||||
{
|
mock.methodReturnsString();
|
||||||
mock.methodReturnsString();
|
result = "foo";
|
||||||
result = "foo";
|
result = new Exception();
|
||||||
result = new Exception();
|
result = "bar";
|
||||||
result = "bar";
|
mock.methodReturnsInt();
|
||||||
mock.methodReturnsInt();
|
result = new int[]{1, 2, 3};
|
||||||
result = new int[] { 1, 2, 3 };
|
mock.methodReturnsString();
|
||||||
mock.methodReturnsString();
|
returns("foo", "bar");
|
||||||
returns("foo", "bar");
|
mock.methodReturnsInt();
|
||||||
mock.methodReturnsInt();
|
result = 1;
|
||||||
result = 1;
|
}};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
assertEquals("Should return foo", "foo", mock.methodReturnsString());
|
assertEquals("Should return foo", "foo", mock.methodReturnsString());
|
||||||
try {
|
try {
|
||||||
|
@ -153,24 +136,23 @@ public class ExpectationsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelegate(@Mocked ExpectationsCollaborator mock) {
|
public void testDelegate(@Mocked ExpectationsCollaborator mock) {
|
||||||
new Expectations() {
|
new Expectations() {{
|
||||||
{
|
mock.methodForDelegate(anyInt);
|
||||||
mock.methodForDelegate(anyInt);
|
result = new Delegate() {
|
||||||
result = new Delegate() {
|
public int delegate(int i) throws Exception {
|
||||||
public int delegate(int i) throws Exception {
|
if (i < 3) {
|
||||||
if (i < 3) {
|
return 5;
|
||||||
return 5;
|
} else {
|
||||||
} else {
|
throw new Exception();
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
};
|
||||||
};
|
}};
|
||||||
|
|
||||||
assertEquals("Should return 5", 5, mock.methodForDelegate(1));
|
assertEquals("Should return 5", 5, mock.methodForDelegate(1));
|
||||||
try {
|
try {
|
||||||
mock.methodForDelegate(3);
|
mock.methodForDelegate(3);
|
||||||
} catch (Exception e) { }
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,9 @@ public class PerformerTest {
|
||||||
model.getInfo();result = "bar";
|
model.getInfo();result = "bar";
|
||||||
collaborator.collaborate("bar"); result = true;
|
collaborator.collaborate("bar"); result = true;
|
||||||
}};
|
}};
|
||||||
|
|
||||||
performer.perform(model);
|
performer.perform(model);
|
||||||
|
|
||||||
new Verifications() {{
|
new Verifications() {{
|
||||||
collaborator.receive(true);
|
collaborator.receive(true);
|
||||||
}};
|
}};
|
||||||
|
|
Loading…
Reference in New Issue