ryzhman
ryzhman

Reputation: 674

Mockito verify the arguments of a method invocation

While writing a Mockito unit-test for a method with several passed in arguments, I want to verify that exact arguments were passed in. Method signature is (for the sake of example):

public void process(String stringParam, int numParam, CustomObject objectParam)

I know that these params must be passed inside:

String stringParam = "line1 \n line2 \n line2 \n"
int numParam = 123;
AnotherCustomObject bank = new AnotherCustomObject(1, "Bank name")
CustomObject objectParam = new CustomObject(1, "Customer name", bank);

verify method looks accordingly:

verify(testObject, times(1)).process(eq(stringParam), eq(numParam), eq(objectParam));

But the result is

Argument(s) are different! Wanted:
...all the details of failure...
Comparison Failure:  <Click to see difference>
...the rest of details...

When you click of Click to see the difference with a hope to see the original problem you see only confusing message Contents are identical (at least in IntelliJ Idea you see this message) enter image description here

Upvotes: 2

Views: 1253

Answers (1)

ryzhman
ryzhman

Reputation: 674

While investigating this case I came over several posts where the root of it was considered to be an incorrectly overriden equals() method either of AnotherCustomObject or CustomObject.

After the investigation, it turned out that it was not a problem. Everything was much more prosaic:

line breaks that were present in the string passed to the method had default Windows encoding (/r/n). Meanwhile, for the virification in Mockito I used a string with only /n breaks (it's a bit weird since I got this string while debugging the method).

At the same time, Inteliji Idea showed both strings in comparison mode as equal.

Update: but it's better to use
System.getProperty("file.separator")

Upvotes: 4

Related Questions