简体   繁体   中英

Have legit Spring beans injected into a mock bean

Anyone knows how can I have normal spring beans injected into my MVC controller?

@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'

I don't want to mock every dependencies here, it seems to be to much work, only external coupling ones. NB: TasksRepository is Spring Data JPA JpaRepository; already tried adding @DataJpaTest and failed (Configuration error: found multiple declarations of @BootstrapWith).

Thanks for any help

Thanks to Joao Dias' remark, here is a (heavier indeed) working code:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM