I'm trying to write a unit test for a controller. Some of the dependencies (such as a service class) I'm mocking with @MockBean. But there are other dependencies I want to just have spring create the beans as usual. Is this possible?
@RunWith(SpringRunner.class)
@WebMvcTest(JwtAuthenticationController.class)
public class JwtControllerTests {
@MockBean
private JwtUserDetailsService jwtUserDetailsService;
@MockBean
private AuthenticationManager authenticationManager;
@ ? ? ?
private JwtTokenUtil jwtTokenUtil
public void auth_Success() throws Exception {
when(
jwtUserDetailsService.loadUserByUsername(anyString())
).thenReturn(adminUserDetails);
RequestBuilder request = MockMvcRequestBuilders
.post("/api/v1/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.content(authBody);
}
}
Controller code:
public class JwtAuthenticationController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
private JwtUserDetailsService jwtUserDetailsService;
}