Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

mockito mock static method

@Test
void givenStaticMethodWithNoArgs_whenMocked_thenReturnsMockSuccessfully() {
    assertThat(StaticUtils.name()).isEqualTo("Baeldung");

    try (MockedStatic<StaticUtils> utilities = Mockito.mockStatic(StaticUtils.class)) {
        utilities.when(StaticUtils::name).thenReturn("Eugen");
        assertThat(StaticUtils.name()).isEqualTo("Eugen");
    }

    assertThat(StaticUtils.name()).isEqualTo("Baeldung");
}
Source by www.baeldung.com #
 
PREVIOUS NEXT
Tagged: #mockito #mock #static #method
ADD COMMENT
Topic
Name
4+6 =