I have a method that does something like this:
public void registerNewFoo( String someId )
{
Foo foo = m_fooList.getFoo( someId );
if ( foo != null )
{
// do something
return;
}
// create new foo
foo = new Foo();
m_fooList.addNewFoo( foo );
}
And my test goes like this:
@Test
public void registerNewFoo_fooAddedToList( @Mocked final Foo mockFoo )
{
new Expectations()
{{
m_mockFooList.getFoo( "test id" );
result = null;
new Foo();
result = mockFoo;
m_mockFooList.addNewFoo( mockFoo );
}};
Bar bar = new Bar( m_mockFooList );
bar.registerNewFoo( "test id" );
}
Running the test gives me this error:
mockit.internal.UnexpectedInvocation: Parameter 0 of addNewFoo(Foo) expected Foo@1a679b7, got Foo@337d0f
If I change the addNewFoo expectation to this:
m_mockFooList.addNewFoo( (Foo)any );
then the test works fine.
The documentation says:
"By default, the exact instance on which instance method invocations will occur is not verified to be the same as the instance used when recording the expectation. That said, instance-specific matching can be obtained by annotating the mock field/parameter as @Injectable, or by using the onInstance(Object) method."
But I am not using @Injectable or onInstance(Object). Am I missing something else?
Using JMockit v1.7 and JUnit v4.8.1.