将合法的 Spring bean 注入模拟 bean
Have legit Spring beans injected into a mock bean
任何人都知道如何将正常的 spring bean 注入到我的 MVC 控制器中?
@WebMvcTest(TaskController.class) class TasksManagerApplicationTests {
@Spy // WRONG
private TasksRepository taskDao;
@MockBean
private AssignmentService service;
@MockBean
private ToDoClient client;
@Autowired
private MockMvc template;
...
NoSuchBeanDefinitionException: No qualifying bean of type 'com.acme.tskmngt.dao.TasksRepository'
我不想在这里模拟每个依赖项,看起来工作量很大,只有外部耦合的。
注意:TasksRepository 是 Spring Data JPA JpaRepository ;已经尝试添加@DataJpaTest 但失败了(配置错误:发现@BootstrapWith 的多个声明)。
感谢您的帮助
感谢 Joao Dias 的评论,这是一个(确实更重但)可执行代码:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Slf4j class TasksManagerApplicationTests {
@Mock
private TaskController controller;
@MockBean
private AssignmentService service;
@MockBean
private ToDoClient client;
@Autowired
private MockMvc template;
@LocalServerPort
private int serverport; // random web server port
@Autowired
private ObjectMapper mapper; // Jackson serializer
任何人都知道如何将正常的 spring bean 注入到我的 MVC 控制器中?
@WebMvcTest(TaskController.class) class TasksManagerApplicationTests {
@Spy // WRONG
private TasksRepository taskDao;
@MockBean
private AssignmentService service;
@MockBean
private ToDoClient client;
@Autowired
private MockMvc template;
...
NoSuchBeanDefinitionException: No qualifying bean of type 'com.acme.tskmngt.dao.TasksRepository'
我不想在这里模拟每个依赖项,看起来工作量很大,只有外部耦合的。 注意:TasksRepository 是 Spring Data JPA JpaRepository ;已经尝试添加@DataJpaTest 但失败了(配置错误:发现@BootstrapWith 的多个声明)。
感谢您的帮助
感谢 Joao Dias 的评论,这是一个(确实更重但)可执行代码:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Slf4j class TasksManagerApplicationTests {
@Mock
private TaskController controller;
@MockBean
private AssignmentService service;
@MockBean
private ToDoClient client;
@Autowired
private MockMvc template;
@LocalServerPort
private int serverport; // random web server port
@Autowired
private ObjectMapper mapper; // Jackson serializer