mardi 25 octobre 2016

"Wanted, but not invoked" even though my parameters match

I'm mocking a class Foo that has this method:

void foo(String string, ComplexBar bar);

My test is as follows:

@Test
public void test1() {
    ComplexBar testBar = new ComplexBar();
    Foo mockFoo = mock(Foo.class);
    TestClass testClass = new TestClass(mockFoo);

    verify(mockFoo, times(1)).foo(eq("myString"), eq(testBar));
}

I want to see whether or not TestClass' constructor calls mockFoo.foo() with the given arguments. ComplexBar has an equals method. Now I'm getting this output:

Wanted but not invoked:
mockFoo.foo(
"myString",
ComplexBar@5a12345
);

However, there were other interactions with this mock:
mockFoo.foo(
"myString",
ComplexBar@6a29498
);

It looks as if the ComplexBar objects don't match up - but a breakpoint reveals that ComplexBar.equals() is never even called!

So I'm trying an argument captor instead:

@Test
public void test1() {
    ComplexBar testBar = new ComplexBar();
    Foo mockFoo = mock(Foo.class);
    TestClass testClass = new TestClass(mockFoo);

    ArgumentCaptor<ComplexBar> captor = ArgumentCaptor.forClass(ComplexBar.class);

    verify(mockFoo, times(1)).foo(eq("myString"), captor.capture());
    assertEquals(testBar, captor.getValue());
}

The result is similar:

Wanted but not invoked:
mockFoo.foo(
"myString",
<Capturing argument>
);

However, there were other interactions with this mock:
mockFoo.foo(
"myString",
ComplexBar@6a29498
);

And again, ComplexBar.equals() is never actually called.

This is with

androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

Which seems to be the only version of Mockito I can get to run on Android.

Why does eq() not call ComplexBar.equals()?

Aucun commentaire:

Enregistrer un commentaire