New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kumick opened this Issue Aug 5, 2014 · 3 comments

Comments

2 participants
@kumick

kumick commented Aug 5, 2014

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

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Aug 5, 2014

Member

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?

Member

rliesenfeld commented Aug 5, 2014

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

This comment has been minimized.

Show comment
Hide comment
@kumick

kumick Aug 6, 2014

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

kumick commented Aug 6, 2014

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

@rliesenfeld rliesenfeld added bug and removed bug labels Aug 7, 2014

@rliesenfeld

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Aug 7, 2014

Member

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

Member

rliesenfeld commented Aug 7, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment