2

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

asked Aug 24, 2018 at 14:55

1 Answer 1

4

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")

answered Aug 24, 2018 at 14:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.