当我想要使用@WebMvcTest测试控制器时,我有一个问题-在应用程序加载时,我得到:
...
Field customUserDetailsService in com.test.config.SecurityConfig required a bean of type 'com.test.security.CustomUserDetailsService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.test.security.CustomUserDetailsService' in your configuration.
...
你知道问题出在哪里吗?排除安全性是一个好的解决方案吗?
有几个类:
@RestController
@AllArgsConstructor
public class EmployeeController {
private final EmployeeService employeeService;
@GetMapping
public List<EmployeeDTO> getEmployees() {
return employeeService.getAllEmployees();
}
}
@WebMvcTest(controllers = EmployeeController.class)
class EmployeeControllerTest {
@Autowired
private MockMvc mockMvc;
@InjectMocks
private EmployeeController employeeController;
@Test
void getEmployees_Success() throws Exception {
// mockMvc.perform(get("/employee/all")).andExpect(status().isOk());
}
发布于 2020-07-21 16:14:53
你在测试中使用的控制器有一个依赖EmployeeService employeeService;
,所以在测试用例中添加如下。
@Mock
EmployeeService employeeService;
https://stackoverflow.com/questions/63009888
复制相似问题