如何解决Spring Boot集成测试因MockMvc的“没有合格的bean”而失败
我想为我的Spring Boot应用程序创建一个集成测试,以检查控制器在发送电子邮件时是否返回正确的HTTP状态。
这是我的测试结果:
@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@Profile("test")
public class EmailControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Rule
public SmtpServerRule smtpServerRule = new SmtpServerRule(2525);
private static final String RESOURCE_PATH = "/mail";
@Test
public void whenValidInput_thenReturns200() throws Exception {
final EmailNotification emailNotification = EmailNotification.builder()
.emailAddress("foo@bar.com")
.subject("TEST_SUBJECT")
.content("TEST_CONTENT")
.build();
mockMvc.perform(post(RESOURCE_PATH)
.contentType(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(emailNotification))
).andExpect(status().isOk());
}
}
但是由于以下原因而失败:
No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我看了有关集成测试的Spring Boot教程,但看不到出什么问题。
这是被测试的控制器:
@RestController
public class EmailController {
private static final Logger LOG = LoggerFactory.getLogger(EmailController.class.getName());
private final EmailService emailService;
@Autowired
public EmailController(EmailService emailService) {
this.emailService = emailService;
}
@PostMapping(value = "/mail",consumes = MediaType.APPLICATION_JSON_VALUE)
public void send(@Valid @RequestBody EmailNotification emailNotification) {
try {
emailService.sendEmail(emailNotification);
} catch (MailException | MessagingException e) {
LOG.error("Error sending email: (recipient address: {}): {}",emailNotification.getEmailAddress(),e.getMessage());
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。