Changes to code to add verifications (#518)

* 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.
This commit is contained in:
Álvaro Fernández González 2016-07-20 09:21:45 +02:00 committed by Grzegorz Piwowarek
parent 2f8dc9d081
commit 11241eabc0
2 changed files with 55 additions and 19 deletions

View File

@ -3,9 +3,12 @@ package org.baeldung.mocks.jmockit;
import java.util.List; import java.util.List;
public interface ExpectationsCollaborator { public interface ExpectationsCollaborator {
void methodForAny(String s, int i, Boolean b, List<String> l); String methodForAny1(String s, int i, Boolean b);
void methodForWith(String s, int i, Boolean b, List<String> l); void methodForAny2(Long l, List<String> lst);
void methodForNulls(String s, List<String> l, List<Integer> m); 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 methodForTimes1();
void methodForTimes2(); void methodForTimes2();
void methodForTimes3(); void methodForTimes3();

View File

@ -14,6 +14,7 @@ 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.integration.junit4.JMockit; import mockit.integration.junit4.JMockit;
@RunWith(JMockit.class) @RunWith(JMockit.class)
@ -24,54 +25,85 @@ public class ExpectationsTest {
public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception { public void testForAny(@Mocked ExpectationsCollaborator mock) throws Exception {
new Expectations() { new Expectations() {
{ {
mock.methodForAny(anyString, anyInt, anyBoolean, (List<String>) any); 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);
} }
}; };
mock.methodForAny("barfooxyz", 0, Boolean.FALSE, new ArrayList<>());
} }
@Test @Test
public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception { public void testForWith(@Mocked ExpectationsCollaborator mock) throws Exception {
new Expectations() { new Expectations() {
{ {
mock.methodForWith(withSubstring("foo"), withNotEqual(1), withNotNull(), withInstanceOf(List.class)); 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));
} }
}; };
mock.methodForWith("barfooxyz", 2, Boolean.TRUE, new ArrayList<>());
} }
@Test @Test
public void testWithNulls(@Mocked ExpectationsCollaborator mock) { public void testWithNulls(@Mocked ExpectationsCollaborator mock) {
// more config
new Expectations() { new Expectations() {
{ {
mock.methodForNulls(anyString, null, (List<Integer>) withNull()); 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());
} }
}; };
mock.methodForNulls("blablabla", new ArrayList<String>(), null);
} }
@Test @Test
public void testWithTimes(@Mocked ExpectationsCollaborator mock) { public void testWithTimes(@Mocked ExpectationsCollaborator mock) {
// more config
new Expectations() { new Expectations() {
{ {
// exactly 2 invocations to foo() are expected // exactly 2 invocations are expected
mock.methodForTimes1(); mock.methodForTimes1();
times = 2; times = 2;
// we expect from 1 to 3 invocations to bar() mock.methodForTimes2(); // "minTimes = 1" is implied
mock.methodForTimes2();
minTimes = 1;
maxTimes = 3;
mock.methodForTimes3(); // "minTimes = 1" is implied
} }
}; };
mock.methodForTimes1(); mock.methodForTimes1();
mock.methodForTimes1(); mock.methodForTimes1();
mock.methodForTimes2(); mock.methodForTimes2();
mock.methodForTimes2();
mock.methodForTimes2();
mock.methodForTimes3(); mock.methodForTimes3();
mock.methodForTimes3();
mock.methodForTimes3();
new Verifications() {
{
// we expect from 1 to 3 invocations
mock.methodForTimes3();
minTimes = 1;
maxTimes = 3;
}
};
} }
@Test @Test
@ -114,6 +146,7 @@ public class ExpectationsTest {
result = 1; result = 1;
} }
}; };
assertEquals("Should return foo", "foo", mock.methodReturnsString()); assertEquals("Should return foo", "foo", mock.methodReturnsString());
try { try {
mock.methodReturnsString(); mock.methodReturnsString();