Reputation: 73
I'm having trouble figuring out how to use the @MockBean annotation in Spring Boot 2.3.0. "user" always returns even though Mockito should have cought the function getCurrentUser.
@MockBean private AuthService authService;
private User mockUser;
@Test
public void test() {
Mockito.when(authService.getCurrentUser()).thenReturn(mockUser);
User userTest = new User();
mockUser = userTest;
User user =authService.getCurrentUser();
}
Upvotes: 1
Views: 4242
Reputation: 6075
This happens because you do the thenReturn(mockUser)
before new User();
The correct code would be something like:
@Test
public void test() {
User userTest = new User();
mockUser = userTest;
Mockito.when(authService.getCurrentUser()).thenReturn(mockUser);
User user = authService.getCurrentUser();
}
Also, you don't need User userTest = new User();
You can do mockUser = new User();
Upvotes: 5