vendredi 29 juillet 2016

Hot verify method arguments with Mockito when they change after method invocation

I have following test code. The code here might look a little bit strange but I took it from a bigger application and removed all unimportant parts.

interface Checker {

    public void check(Set set);
}

public void methodToTest(Checker checker, Set things){
    checker.check(things);
    things.add("3");      
}


@Test
public void test() {

    Checker checker = mock(Checker.class);
    Set argSet = Sets.newHashSet("1", "2");
    Set exSet = Sets.newHashSet("1", "2");

    methodToTest(checker, argSet);

    verify(checker).check(exSet);
}

Comparison Failure: Expected :checker.check([1, 2]); Actual :checker.check([1, 2, 3]);

After debugging I found out that Mockito stores the argument of the method call by reference. So it points to the argSet which gets changed before verifying.

How can I solve that issue. Is there a way to somehow tell mockito to store the argument state at the point of method invocation?

Aucun commentaire:

Enregistrer un commentaire