Skip to content

"description" not printing when verify args don't match #1712

@tir38

Description

@tir38

Mockito version 2.27.0

I have a test that uses description inside a verify method. The description is correctly printing when the test fails because we don't interact with the mock

import static org.mockito.Mockito.description;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import org.junit.Test;

public class ExampleTest {
    @Test
    public void shouldFailWithDescription() {
        Dependency dependency = spy(Dependency.class);
        SystemUnderTest systemUnderTest = new SystemUnderTest();
        systemUnderTest.doSomething(dependency);
        verify(dependency, description("Failed to call doSomethingElse")).doSomethingElse(false);
    }

    static class SystemUnderTest {
        void doSomething(Dependency dependency) {
            // dependency.doSomethingElse(true); // <---- TEST FAILS BECAUSE WE DON'T INTERACT WITH MOCK
        }
    }

    static class Dependency {
        void doSomethingElse(boolean value) {
            // ...
        }
    }
}

Results in this failure message:

org.mockito.exceptions.base.MockitoAssertionError: Failed to call doSomethingElse <---- SEE OUR DESCRIPTION

Wanted but not invoked:
dependency.doSomethingElse(false);
-> at com.example.ExampleTest$Dependency.doSomethingElse(ExampleTest.java:28)
Actually, there were zero interactions with this mock.

In this case the test failed because there were no interactions with this mock. However if we update the SUT so that it calls the method with the wrong argument:

public class ExampleTest {
    @Test
    public void shouldFailWithDescription() {
        Dependency dependency = spy(Dependency.class);
        SystemUnderTest systemUnderTest = new SystemUnderTest();
        systemUnderTest.doSomething(dependency);
        verify(dependency, description("Failed to call doSomethingElse")).doSomethingElse(false);
    }

    static class SystemUnderTest {
        void doSomething(Dependency dependency) {
             dependency.doSomethingElse(true);  // <---- TEST FAILS BECAUSE WE PASS WRONG ARGS
        }
    }

    static class Dependency {
        void doSomethingElse(boolean value) {
            // ...
        }
    }
}

The test still fails. But this time it does not include the description:

Argument(s) are different! Wanted: <----- WE SHOULD SEE DESCRIPTION
dependency.doSomethingElse(false);
-> at com.example.ExampleTest$Dependency.doSomethingElse(ExampleTest.java:28)
Actual invocation has different arguments:
dependency.doSomethingElse(true);
-> at com.example.ExampleTest$SystemUnderTest.doSomething(ExampleTest.java:21)

Comparison Failure: 
Expected :dependency.doSomethingElse(false);
Actual   :dependency.doSomethingElse(true);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions