bael-3314:update jmockit articles (#14320)
This commit is contained in:
parent
f372563ca2
commit
eea21ae2a0
|
@ -67,7 +67,7 @@
|
||||||
<javafaker.version>0.15</javafaker.version>
|
<javafaker.version>0.15</javafaker.version>
|
||||||
<jukito.version>1.5</jukito.version>
|
<jukito.version>1.5</jukito.version>
|
||||||
<easymock.version>3.5.1</easymock.version>
|
<easymock.version>3.5.1</easymock.version>
|
||||||
<jmockit.version>1.41</jmockit.version>
|
<jmockit.version>1.49</jmockit.version>
|
||||||
<jimf.version>1.1</jimf.version>
|
<jimf.version>1.1</jimf.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,12 @@ public class AdvancedCollaborator {
|
||||||
i = string.length();
|
i = string.length();
|
||||||
}
|
}
|
||||||
public String methodThatCallsPrivateMethod(int i){
|
public String methodThatCallsPrivateMethod(int i){
|
||||||
return privateMethod() + i;
|
return protectedMethod() + i;
|
||||||
}
|
}
|
||||||
public int methodThatReturnsThePrivateField(){
|
public int methodThatReturnsThePrivateField(){
|
||||||
return privateField;
|
return privateField;
|
||||||
}
|
}
|
||||||
private String privateMethod(){
|
protected String protectedMethod(){
|
||||||
return "default:";
|
return "default:";
|
||||||
}
|
}
|
||||||
class InnerAdvancedCollaborator{}
|
class InnerAdvancedCollaborator{}
|
||||||
|
|
|
@ -1,37 +1,33 @@
|
||||||
package com.baeldung.jmockit;
|
package com.baeldung.jmockit;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import mockit.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.io.Serializable;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.baeldung.jmockit.AdvancedCollaborator;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertNull;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import mockit.Deencapsulation;
|
public class AdvancedCollaboratorIntegrationTest {
|
||||||
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)
|
interface IList<T> extends List<T> {}
|
||||||
public class AdvancedCollaboratorIntegrationTest<MultiMock extends List<String> & Comparable<List<String>>> {
|
interface IComparator extends Comparator<Integer>, Serializable {}
|
||||||
|
static class MultiMock {
|
||||||
|
IList<?> get() { return null; }
|
||||||
|
IComparator compareTo() { return null; }
|
||||||
|
}
|
||||||
|
|
||||||
@Tested
|
@Tested
|
||||||
private AdvancedCollaborator mock;
|
private AdvancedCollaborator mock;
|
||||||
|
|
||||||
@Mocked
|
|
||||||
private MultiMock multiMock;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToMockUpPrivateMethod() {
|
public void testToMockUpPrivateMethod() {
|
||||||
new MockUp<AdvancedCollaborator>() {
|
new MockUp<AdvancedCollaborator>() {
|
||||||
@Mock
|
@Mock
|
||||||
private String privateMethod() {
|
protected String protectedMethod() {
|
||||||
return "mocked: ";
|
return "mocked: ";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -52,40 +48,26 @@ public class AdvancedCollaboratorIntegrationTest<MultiMock extends List<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToSetPrivateFieldDirectly() {
|
public void testToSetPrivateFieldDirectly(@Injectable("10") int privateField) {
|
||||||
Deencapsulation.setField(mock, "privateField", 10);
|
assertEquals(10, privateField);
|
||||||
assertEquals(10, mock.methodThatReturnsThePrivateField());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToGetPrivateFieldDirectly() {
|
public void testToGetPrivateFieldDirectly() {
|
||||||
int value = Deencapsulation.getField(mock, "privateField");
|
assertEquals(5, mock.methodThatReturnsThePrivateField());
|
||||||
assertEquals(5, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void testMultipleInterfacesWholeTest() {
|
public void testMultipleInterfacesWholeTest(@Mocked MultiMock multiMock) {
|
||||||
new Expectations() {
|
new Expectations() {
|
||||||
{
|
{
|
||||||
multiMock.get(5); result = "foo";
|
multiMock.get(); result = null;
|
||||||
multiMock.compareTo((List<String>) any); result = 0;
|
multiMock.compareTo(); result = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
assertEquals("foo", multiMock.get(5));
|
assertNull(multiMock.get());
|
||||||
assertEquals(0, multiMock.compareTo(new ArrayList<>()));
|
assertNull(multiMock.compareTo());
|
||||||
}
|
}
|
||||||
|
|
||||||
@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<>()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,17 @@
|
||||||
package com.baeldung.jmockit;
|
package com.baeldung.jmockit;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
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.Verifications;
|
import mockit.Verifications;
|
||||||
import mockit.integration.junit4.JMockit;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
@RunWith(JMockit.class)
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class ExpectationsIntegrationTest {
|
public class ExpectationsIntegrationTest {
|
||||||
|
|
||||||
|
@ -91,15 +85,11 @@ public class ExpectationsIntegrationTest {
|
||||||
@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(with(new Delegate<Object>() {
|
||||||
@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
|
|
||||||
public void describeTo(Description description) {
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
}};
|
}};
|
||||||
mock.methodForArgThat(new Model());
|
mock.methodForArgThat(new Model());
|
||||||
|
|
|
@ -1,18 +1,13 @@
|
||||||
package com.baeldung.jmockit;
|
package com.baeldung.jmockit;
|
||||||
|
|
||||||
import mockit.*;
|
|
||||||
import mockit.integration.junit4.JMockit;
|
|
||||||
import com.baeldung.testCase.LoginController;
|
import com.baeldung.testCase.LoginController;
|
||||||
import com.baeldung.testCase.LoginDao;
|
import com.baeldung.testCase.LoginDao;
|
||||||
import com.baeldung.testCase.LoginService;
|
import com.baeldung.testCase.LoginService;
|
||||||
import com.baeldung.testCase.UserForm;
|
import com.baeldung.testCase.UserForm;
|
||||||
import org.hamcrest.BaseMatcher;
|
import mockit.*;
|
||||||
import org.hamcrest.Description;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
@RunWith(JMockit.class)
|
|
||||||
public class LoginControllerIntegrationTest {
|
public class LoginControllerIntegrationTest {
|
||||||
|
|
||||||
@Injectable
|
@Injectable
|
||||||
|
@ -110,16 +105,10 @@ public class LoginControllerIntegrationTest {
|
||||||
loginService.login((UserForm) any);
|
loginService.login((UserForm) any);
|
||||||
result = true;
|
result = true;
|
||||||
// complex matcher
|
// complex matcher
|
||||||
loginService.setCurrentUser(withArgThat(new BaseMatcher<String>() {
|
loginService.setCurrentUser(with(new Delegate<String>() {
|
||||||
@Override
|
|
||||||
public boolean matches(Object item) {
|
public boolean matches(Object item) {
|
||||||
return item instanceof String && ((String) item).startsWith("foo");
|
return item instanceof String && ((String) item).startsWith("foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void describeTo(Description description) {
|
|
||||||
//NOOP
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,8 @@
|
||||||
package com.baeldung.jmockit;
|
package com.baeldung.jmockit;
|
||||||
|
|
||||||
import com.baeldung.jmockit.Collaborator;
|
|
||||||
import com.baeldung.jmockit.Model;
|
|
||||||
import com.baeldung.jmockit.Performer;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import mockit.*;
|
import mockit.*;
|
||||||
import mockit.integration.junit4.JMockit;
|
import org.junit.Test;
|
||||||
|
|
||||||
@RunWith(JMockit.class)
|
|
||||||
public class PerformerIntegrationTest {
|
public class PerformerIntegrationTest {
|
||||||
|
|
||||||
@Injectable
|
@Injectable
|
||||||
|
|
|
@ -1,20 +1,9 @@
|
||||||
package com.baeldung.jmockit;
|
package com.baeldung.jmockit;
|
||||||
|
|
||||||
import com.baeldung.jmockit.Collaborator;
|
import mockit.*;
|
||||||
import com.baeldung.jmockit.Model;
|
|
||||||
import com.baeldung.jmockit.Performer;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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 {
|
public class ReusingIntegrationTest {
|
||||||
|
|
||||||
@Injectable
|
@Injectable
|
||||||
|
|
Loading…
Reference in New Issue