Search code examples
javajmockit

JMockit - How to verify a method was invoked on a local varaible


This is the code I am trying to test with JMockit:

public void unitUnderTest() {
  doSomething();
}

private void doSomething() {
  Foo foo = new Foo();
  foo.setExtraFooness("its extra fooness");
  ...
  ...
}

Is it possible to verify that foo.setExtraFooness("its extra fooness"); was called?


Solution

  • You can always verify calls on mocked methods:

    @Test
    public void exampleTest(@Mocked Foo foo) {
        new ClassUnderTest().unitUnderTest();
    
        new Verifications() {{ foo.setExtraFooness("whatever"); }};
    }
    

    That said, it's generally better to avoid mocking when you really want to verify state. If the foo object can be obtained from the object under test, then the test should simply write a conventional assertion to verify its state.