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