From 28aef2338d4f5bac91790f55826691bb2b4e443e Mon Sep 17 00:00:00 2001 From: Xiaojian Zheng Date: Wed, 13 Mar 2024 00:18:25 +0800 Subject: [PATCH] fix java-and-spring-unit-test.md --- docs/posts/java-and-spring-unit-test.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/posts/java-and-spring-unit-test.md b/docs/posts/java-and-spring-unit-test.md index b5279e1..7dab5eb 100644 --- a/docs/posts/java-and-spring-unit-test.md +++ b/docs/posts/java-and-spring-unit-test.md @@ -242,8 +242,8 @@ public class UserServiceSpyTest { @Test void shouldReturnCorrectUser() { - when(userDao.getUserNameById(anyString())) - .thenAnswer(invocationOnMock -> "mockUser: " + invocationOnMock.getArgument(0)); + doAnswer(invocationOnMock -> "mockUser: " + invocationOnMock.getArgument(0)) + .when(userDao).getUserNameById(anyString()); User user = userService.getUserById("1"); Assertions.assertEquals(new User("1", "mockUser: 1"), user); Assertions.assertEquals(user.getName().length(), userDao.getAgeByName(user.getName())); @@ -414,13 +414,13 @@ public class UserServiceMoodTest { @Test void shouldHappyWhenEven() { - Mockito.when(userService.getCurrentTimeMillis()).thenReturn(2L); + Mockito.doReturn(2L).when(userService).getCurrentTimeMillis(); IntStream.range(0, 100).forEach(i -> Assertions.assertEquals("happy", userService.getUserMood())); } @Test void shouldSadWhenOdd() { - Mockito.when(userService.getCurrentTimeMillis()).thenReturn(1L); + Mockito.doReturn(1L).when(userService).getCurrentTimeMillis(); IntStream.range(0, 100).forEach(i -> Assertions.assertEquals("sad", userService.getUserMood())); } } @@ -516,8 +516,8 @@ public class UserControllerTest { @Test public void shouldReturnCorrectUser() throws Exception { - when(userDao.getUserNameById(anyString())) - .thenAnswer(invocationOnMock -> "mockUser: " + invocationOnMock.getArgument(0)); + doAnswer(invocationOnMock -> "mockUser: " + invocationOnMock.getArgument(0)) + .when(userDao).getUserNameById(anyString()); ObjectMapper om = new ObjectMapper(); mockMvc.perform(MockMvcRequestBuilders.get("/user/10")) .andDo(MockMvcResultHandlers.print())