1

When debugging a test I realized that something was interfering with Mockito well functioning. Somehow, the inclusion of breakpoints in specific classes leads to a different output.

I try to illustrate it with a simple example.

public class MockitoTrial {

  @Test
  public void simpleTest() {
    var func = Mockito.mock(Function.class);
    Entry<String, Integer> entry = new SimpleEntry<>("one", 1);
    when(func.apply(eq(Entry.class))).thenReturn(entry);

    assertThat(func.apply(Entry.class)).isEqualTo(entry);
  }

}

If I set a breakpoint for instance in org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.interceptAbstract, and rerun it in debug-mode, the test fails.

It seems apparently unrelated to the IDE as it happens when debugging remotely as well.

The library versions I am using:

assertj-core-3.22.0
junit-jupiter-api-5.8.2
mockito-core-4.5.1
Alfredo Diaz
  • 628
  • 1
  • 6
  • 13

1 Answers1

0

The question pointed out by Jonasz, gives a good clue.

If I instead set the breakpoint in org.mockito.internal.handler.MockHandlerImpl.handle, the test does already pass even in debug-mode.

Now, I add a watcher for the expression invocation.getMock().toString() and rerun it in debug-mode. Doing that, I got the test to fail again.

Mockito is very sensitive to calls to the mock methods in between its processing.

Alfredo Diaz
  • 628
  • 1
  • 6
  • 13