You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As per title, argThat(sameBeanAs()) does not describe very well the failure.
The result looks like the following:
Argument(s) are different! Wanted:
service.execute(
{
"parameterValue": "wrongParam"
}
);
-> at com.shazam.shazamcrest.MatcherAssertMockitoVerifyTest.testMockitoFailingVerifyOneParameter(MatcherAssertMockitoVerifyTest.java:40)
Actual invocation has different arguments:
service.execute(
com.shazam.shazamcrest.MatcherAssertMockitoVerifyTest$ParameterWrapper@2aa5fe93
);
-> at com.shazam.shazamcrest.MatcherAssertMockitoVerifyTest.testMockitoFailingVerifyOneParameter(MatcherAssertMockitoVerifyTest.java:36)
It can be tested with the following test (mockito needs to be added to the pom.xml):
package com.shazam.shazamcrest;
import static com.shazam.shazamcrest.MatcherAssertMockitoVerifyTest.ParameterWrapper.parameterWrapper;
import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.argThat;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
public class MatcherAssertMockitoVerifyTest {
@Test
public void testMockitoSuccessfulVerifyOneParameter() {
// Given
Service serviceMock = Mockito.mock(Service.class);
// When
serviceMock.execute(parameterWrapper("param"));
// Then
Mockito.verify(serviceMock).execute(
argThat(sameBeanAs(parameterWrapper("param")))
);
}
@Test
public void testMockitoFailingVerifyOneParameter() {
// Given
Service serviceMock = Mockito.mock(Service.class);
// When
serviceMock.execute(parameterWrapper("param"));
// Then
try {
Mockito.verify(serviceMock).execute(
argThat(sameBeanAs(parameterWrapper("wrongParam")))
);
Assert.fail("Parameter are different but test did not fail.");
} catch (ArgumentsAreDifferent e) {
assertEquals(e.getMessage(),
"\n" +
"Argument(s) are different! Wanted:\n" +
"service.execute(\n" +
" {\n" +
" \"parameterValue\": \"wrongParam\"\n" +
"}\n" +
");\n" +
"-> at com.shazam.shazamcrest.MatcherAssertMockitoVerifyTest.testMockitoFailingVerifyOneParameter(MatcherAssertMockitoVerifyTest.java:40)\n" +
"Actual invocation has different arguments:\n" +
"service.execute(\n" +
" {\n" +
" \"parameterValue\": \"wrongParam\"\n" +
"}\n" +
");\n" +
"-> at com.shazam.shazamcrest.MatcherAssertMockitoVerifyTest.testMockitoFailingVerifyOneParameter(MatcherAssertMockitoVerifyTest.java:36)\n");
}
}
public interface Service {
void execute(ParameterWrapper parameterWrapper);
}
public static class ParameterWrapper {
private String parameterValue;
public ParameterWrapper(String parameterValue) {
this.parameterValue = parameterValue;
}
public static ParameterWrapper parameterWrapper(String value) {
return new ParameterWrapper(value);
}
}
}
The text was updated successfully, but these errors were encountered:
As per title,
argThat(sameBeanAs())
does not describe very well the failure.The result looks like the following:
It can be tested with the following test (mockito needs to be added to the pom.xml):
The text was updated successfully, but these errors were encountered: