jeudi 5 février 2015

MissingInvocation of Expected static method with mocked instance parameter

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.useVisitor(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#useVisitor(SimpleClass simple)
with arguments: SimpleClass@45490852




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 .


Aucun commentaire:

Enregistrer un commentaire