【发布时间】:2018-05-01 20:59:24
【问题描述】:
我有这个 REST 控制器:
package com.company.rest;
@RestController
@RequestMapping("/v1/orders")
public class OrderController {
@Autowired
private OrderService orderService;
...
作为OrderService 实现:
package com.company.service.impl;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private MessageService messageService;
...
和MessageService 实现:
package com.company.service.impl;
import org.springframework.mail.javamail.JavaMailSender;
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
public JavaMailSender emailSender;
...
这在开发环境中完美运行,但我对OrderController 进行了这个单元测试(基于this 教程):
package com.company.test;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class)
@WebAppConfiguration
public class OrderTest {
private MockMvc mockMvc;
@Autowired
private OrderService orderService;
...
导致:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
为什么这种依赖在生产中得到满足,但在测试中却没有?我需要做什么才能让这个单元测试成功地注入(或模拟)JavaMailSender 实现?
【问题讨论】:
-
你能把pom文件中提到这个依赖的部分贴出来
标签: java spring unit-testing spring-boot junit