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

Mockito verify message not accurate if type is fields are of Object #3269

Open
LoganDev99 opened this issue Feb 14, 2024 · 0 comments
Open

Comments

@LoganDev99
Copy link

Hi,

mockito: 5.10.0
jdk: Java 11
os: Windows 10

The mockito verify error message isn't accurate when the type of fields are Object and the object to match in mockito.verify() have a type mismatch. In my example I pass in a int 1 and a string 1. But the error message is wrong and only shows int 1 for both wanted and actual.

Run mvn clean test The test will failure. Which is correct. But the error message isn't correct.

Output:

Argument(s) are different! Wanted:
myService.myMethod(MyClass(myValue=1));
-> at com.example.mockitoissue.MyService.myMethod(MockitoissueApplicationTests.java:27)
Actual invocations have different arguments:
myService.myMethod(MyClass(myValue=1));
-> at com.example.mockitoissue.MockitoissueApplicationTests.contextLoads(MockitoissueApplicationTests.java:18)
Wanted should show a string value the number 1 for myValue=1. Instead both are shown as the same.

Clicking in accurately shows the difference being a new line.

Sample Repo
https://github.com/LoganDev99/mockito-inaccurate-error

Also snippet for searchability.

class MockitoissueApplicationTests {

	@Test
	void test() {
		MyService myService = mock(MyService.class);

		MyClass myClass1 = new MyClass(1);
		MyClass myClass2 = new MyClass("1");

		myService.myMethod(myClass1);
		verify(myService).myMethod(myClass2);
	}

}

class MyService {

	public void myMethod(MyClass myClass) {
		System.out.println(myClass);
	}
}

@Value
class MyClass {
	Object myValue;
}

check that

  • [ x] The mockito message in the stacktrace have useful information, but it didn't help
  • [x ] The problematic code (if that's possible) is copied here;
    Note that some configuration are impossible to mock via Mockito
  • [ x] Provide versions (mockito / jdk / os / any other relevant information)
  • [x ] Provide a Short, Self Contained, Correct (Compilable), Example of the issue
    (same as any question on stackoverflow.com)
  • [x ] Read the contributing guide
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