spring-boot - Spring Boot 测试:未加载 JavaMailSender 属性
问题描述
我有一个 JUnit5 单元测试来测试我的 Spring bean 的 sendig 电子邮件。我使用spring-boot-starter-parent 2.5.0
及其spring-boot-starter-mail
作为依赖项。
在我的/src/test/resources
目录中,我有一个application.properties
包含spring.mail
属性的文件。当我运行测试时,bean 被实例化并自动装配,但没有加载属性。javaMailProperties
教室里面JavaMailSender
是空的。
我不想使用@SpringBootTest
,因为 Spring 会尝试设置所有自动配置的 bean,这些 bean 对于某些 DB 数据源失败。但它应该以这种方式工作。我错过了什么?不MailSender
支持自动装配属性吗?
@ExtendWith(SpringExtension.class)
@TestPropertySource("classpath:application.properties")
public class EmailClientTest
{
@TestConfiguration
static class EmailClientTestConfiguration
{
@Bean
public MailSender emailSender() {
return new JavaMailSenderImpl();
}
@Bean
public EmailClient emailClient(MailSender emailSender) {
return new EmailClient(emailSender);
}
}
@Autowired
private EmailClient emailClient;
@Test
void sendEmail() {
//...
}
}
解决方案
推荐阅读
- flask - Flask:渲染前扩展模板
- r - 使用 for 循环在 ggplot2 中添加层
- automated-tests - 如何自动化测试虚拟现实应用程序?
- list - Flutter:检查[{“key”:“value”}]列表中是否存在键
- webpack - 使用webpack可以安装git和使用git命令吗
- angular - 通过指向在 Angular 中为 glTF 文件指定 src 的资源来加载 glTF 模型
- html - 使用 inline-flex 右对齐 div/文本
- typescript - 如何从另一个接口的值中提取类型
- r-caret - R插入符号中的两部分模型
- javascript - 如何在 HTML 表格和 d3 条形图中创建下拉菜单