Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using argThat(sameBeanAs()) on mockito message description gets lost #28

Open
antonioalonzi opened this issue Mar 18, 2018 · 1 comment

Comments

@antonioalonzi
Copy link

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);
        }
    }

}

@antonioalonzi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant