jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
461 stars 239 forks source link

Using withCapture prevents missing invocation error when times = 1 specified #27

Closed kumick closed 10 years ago

kumick commented 10 years ago

Trying to use withCapture to validate the object passed to a method, but if the method is never called a null pointer exception is raised on the assert statement following the withCapture instead of a missing invocation error. The missing invocation is a preferred response since it clearly states that the method was never called.

Code snipet where ops is a mocked object. Ops is not used in any Expectations prior to verification. And the current implementation does not call ops.add(alarm). new Verifications() {{ Alarm alarm; ops.add(alarm = withCapture()); times = 1; assertEquals(... }}

Using version 1.10.

rliesenfeld commented 10 years ago

Ok, so I wrote the following test to try and reproduce the problem:

static class Ops { void add(Alarm alarm) {} }
static class Alarm {}

@Test
public void issue27(@Mocked final Ops ops) {
    // ops.add(new Alarm());

   new Verifications() {{
       Alarm alarm;
       ops.add(alarm = withCapture());
       times = 1;
   }};
}

However, the test fails as expected, with: mockit.internal.MissingInvocation: Missing 1 invocation to: mockit.WithCaptureTest$Ops#add(mockit.WithCaptureTest$Alarm alarm) and no NPE. Could you run it and see if it throws a NPE?

kumick commented 10 years ago

Your test needs an assert that tries to reference the value you just captured. Which will cause the NullPointerException.

rliesenfeld commented 10 years ago

Ok, I will see what can be done about it.