Home > AI > Backend > SpringBoot > Mockito >

Mockito.when().thenReturn()

Example 1

class Demo12ApplicationTests {

	@Test
	void contextLoads() {
	}



    @Test
    public void givenCountMethodMocked_WhenCountInvoked_ThenMockedValueReturned() {
        UserRepository mockUserRepository = Mockito.mock(UserRepository.class);
        Mockito.when(mockUserRepository.count()).thenReturn(111L);

        long userCount = mockUserRepository.count();

        Assert.assertEquals(111L, userCount);
        Mockito.verify(mockUserRepository).count();
    }
}

Firstly, it has nothing to do with @SpringBootTest

Secondly, Mockito.when().thenReturn() means giving a return value for the method.

Leave a Reply