I am using JMockIt 1.8, and I have the following classes:
public class SimpleUser {
public static void useSimple(final SimpleClass simple) {
System.out.println("useSimple called");
}
public void createAndUse() {
final SimpleClass simple = new SimpleClass();
simple.method();
SimpleUser.useSimple(simple);
}
}
and
public class SimpleClass {
public void method() {
}
}
With the following test class:
public class Tester {
@Mocked SimpleClass simple;
@Test
public void test() {
new Expectations(SimpleUser.class) {
{
new SimpleClass();
simple.method();
SimpleUser.useSimple(simple);
}
};
SimpleUser user = new SimpleUser();
user.createAndUse();
}
}
And this test passes.
However, when I remove the call to simple.method()
in SimpleUser
and the expectation for simple.method()
in Tester
, the test errors with:
mockit.internal.MissingInvocation: Missing invocation of
SimpleUser#useSimple(SimpleClass simple)
with arguments: SimpleClass@45490852
I can cause this modified version of the test to pass by changing the expectation from SimpleUser.useSimple(simple)
to SimpleUser.useSimple((SimpleClass) any)
, but I would like to assert that the instance being passed through is correct.
Why does the behavior change depending on whether method()
is called and how can I ensure that SimpleUser.useSimple()
is called with the SimpleClass
created in createAndUse()
, without resorting to calling a method within SimpleClass
.