Mockito can mock static methods! · Andrei Solntsev

PHOTO EMBED

Mon Nov 30 2020 16:06:12 GMT+0000 (UTC)

Saved by @edwgarci

@Test
void lookMomICanMockStaticMethods() {
  assertThat(Buddy.name()).isEqualTo("John");

  try (MockedStatic<Buddy> theMock = Mockito.mockStatic(Buddy.class)) {
    theMock.when(Buddy::name).thenReturn("Rafael");
    assertThat(Buddy.name()).isEqualTo("Rafael");
  }

  assertThat(Buddy.name()).isEqualTo("John");
}
content_copyCOPY

testing static methods

https://asolntsev.github.io/en/2020/07/11/mockito-static-methods/