Using withCapture followed by assertEquals gives MissingInvocation of equals #361

Closed
aaamos opened this Issue Nov 22, 2016 · 0 comments

Projects

None yet

2 participants

@aaamos
aaamos commented Nov 22, 2016
  • Version of JMockit that was used: 1.19, also confirmed in 1.29

  • Description of the problem or enhancement request:

I'm using withCapture() in a Verifications block to make sure a called method uses the expected parameter. Then I'd like to use assertEquals to make sure the captured parameter is what I expect - in my case a LocalDateTime; I haven't been able to reproduce the same issue with other classes.

Instead of the assertEquals telling me what I want to know, though, I get the following:

mockit.internal.MissingInvocation: Missing invocation of: java.time.LocalDateTime#equals(Object)

It's as though JMockit is considering the call to equals() used by assertEquals() part of the code to be verified.

Here's a simplified, self-contained test case demonstrating the bug:

package temp;

import static org.junit.Assert.assertEquals;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import org.junit.Test;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Tested;
import mockit.Verifications;

public class WithCaptureTest {

  @Tested
  private TestedClass testedInstance;

  @Test
  public void testWithCaptureBug(@Mocked MockedClass mockedInstance) {
    LocalDateTime startOfJanuary = LocalDateTime.of(2016, 1, 1, 0, 0);
    LocalDateTime startOfFebruary = LocalDateTime.of(2016, 2, 1, 0, 0);
    // @autoformat:off
    new Expectations(LocalDateTime.class) {{
      LocalDateTime.ofInstant((Instant) any, (ZoneId) any); result = startOfJanuary;
    }}; // @autoformat:on

    testedInstance.doSomething();

    // @autoformat:off
    new Verifications() {{
      LocalDateTime usedDateTime;
      mockedInstance.doSomethingWithDateTime(usedDateTime = withCapture());
      assertEquals(usedDateTime, startOfFebruary);
    }}; // @autoformat:on
  }

  public static class TestedClass {

    public void doSomething() {
      Date date = new Date();
      LocalDateTime adjustedDateTime = LocalDateTime.ofInstant(date.toInstant(),
          ZoneId.systemDefault()).plusMonths(1);
      MockedClass toBeMocked = new MockedClass();
      toBeMocked.doSomethingWithDateTime(adjustedDateTime);
    }
  }

  public static class MockedClass {

    public void doSomethingWithDateTime(LocalDateTime dateTime) {
      // do something...
    }
  }
}
@rliesenfeld rliesenfeld self-assigned this Nov 22, 2016
@rliesenfeld rliesenfeld added the bug label Nov 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment