Java Springboot测试未能在Junit 5中加载ApplicationContext
我正在尝试使用Junit5为特定服务类创建单元/集成测试,以避免整个项目过载 因此,在这里我尝试运行Java Springboot测试未能在Junit 5中加载ApplicationContext,java,spring-boot,junit5,Java,Spring Boot,Junit5,我正在尝试使用Junit5为特定服务类创建单元/集成测试,以避免整个项目过载 因此,在这里我尝试运行EmailService,其中包含其依赖类,但我得到了java.lang.IllegalStateException:无法加载ApplicationContext。创建名为“emailSenderService”的bean时出错。没有类型为“org.springframework.mail.javamail.JavaMailSender”的合格bean可用:至少需要1个符合autowire候选条件
EmailService
,其中包含其依赖类,但我得到了java.lang.IllegalStateException:无法加载ApplicationContext。创建名为“emailSenderService”的bean时出错。没有类型为“org.springframework.mail.javamail.JavaMailSender”的合格bean可用:至少需要1个符合autowire候选条件的bean。
我必须运行整个应用程序才能测试单个服务吗
build.yml
{
testImplementation ('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation "org.junit.jupiter:junit-jupiter:5.4.1"
}
服务:
public class EmailSenderService {
private final JavaMailSender sender;
private final SpringTemplateEngine templateEngine;
private final MessageSource i18n;
public EmailSenderService(JavaMailSender sender, SpringTemplateEngine templateEngine,
@Qualifier("messageSource") MessageSource i18n) {
this.sender = sender;
this.templateEngine = templateEngine;
this.i18n = i18n;
}
}
测试等级:
@SpringBootTest(
classes = {EmailSenderService.class}
)
@ExtendWith({SpringExtension.class})
class EmailServiceTest {
private static GreenMail smtp;
@Autowired
private EmailSenderService mailService;
@BeforeAll
static void init() {
smtp = new GreenMail(new ServerSetup(3026,null,"smtp"));
smtp.start();
}
@AfterAll
static void tearDown() {
smtp.stop();
}
@BeforeEach
void clearUp() throws FolderException {
smtp.purgeEmailFromAllMailboxes();
}
@Test
void testNewBidRequestEmail() throws MessagingException {
EmailMessageTemplateDto contact = new EmailMessageTemplateDto("test","test@test.com","test message");
mailService.sendUserContactEmail(contact);
Assertions.assertTrue(smtp.waitForIncomingEmail(1));
}
}
错误:
2019-04-03 14:56:06.146警告732---[主要]
o、 s.w.c.s.GenericWebApplicationContext:遇到异常
在上下文初始化期间-取消刷新尝试:
org.springframework.beans.factory.unsatifiedDependencyException:
创建名为“emailSenderService”的bean时出错:不满意
通过构造函数参数0表示的依赖关系;嵌套异常
是org.springframework.beans.factory.NoSuchBean定义异常:否
类型的限定bean
“org.springframework.mail.javamail.JavaMailSender”可用:应为
至少1个符合autowire候选资格的bean。附属国
注释:{}2019-04-0314:56:06.153错误732---[主要]o.s.b.d.记录故障分析报告员: ***************************应用程序无法启动
说明: 中构造函数的参数0 com.server.server.service.EmailSenderService需要一个 键入“org.springframework.mail.javamail.JavaMailSender”,但无法 找到了 行动: 考虑定义类型为的bean “org.springframework.mail.javamail.JavaMailSender”在 配置 2019-04-03 14:56:06.159错误732---[main] o、 s.test.context.TestContextManager:在 允许TestExecutionListener [org.springframework.test.context.web。ServletTestExecutionListener@342c38f8] 准备测试实例 [com.server.server.test.junit。EmailServiceTest@4c7a078] java.lang.IllegalStateException:无法在处加载ApplicationContext org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.5.释放罐:5.1.5.释放]在 org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
问题是您实际上没有可用的
JavaMailSender
(而且在测试期间您也不需要一个真正的)。您有四种选择:
- 在测试配置中注册一个mock/stub
beanJavaMailSender
- 使用自动配置使您的
本身EmailSenderService
,如果没有存根,则注册存根(这通常是我测试“系统是否发送事务性邮件?”)时所做的操作)@ConditionalOnBean(JavaMailSender.class)
- 在本例中,由于您实际上正在尝试测试
本身,请在EmailSenderService
属性(或注释)中设置应用程序测试
spring.mail.host:localhost
- 这里根本不用springdi。构造函数注入的主要优点是,您可以手动实例化bean,并且可以
更新new
及其依赖项EmailSenderService
如果我了解您测试的预期范围,#3可能是您的解决方案。1选项:如果我使用模拟的
JavaMailSender
,它是否能够发送电子邮件?对于第二个选项,在提问之前,我将尝试了解更多关于@ConditionalOnBean
注释的信息。关于第三个选项,在提交此问题之前,我已经有了一个默认的application.yml
文件,其中包含spring.mail.host:localhost
。第四个选项,我是否需要检索主spring应用程序的任何参数来创建新的EmailSenderService
?@LunaticJape如果这样做,它将不会发送真正的邮件,这就是为什么它可能不是您想要测试的最佳选项。在第三个选项中,您应该得到一个JavaMailSender
;确保已包含spring boot starter mail
,如果它仍然不工作,请使用--debug
获取自动配置报告并发布它。对于选项4,它取决于您想要测试的内容;您根本不需要Spring,但测试消息和模板引擎在Spring上下文中是否正确配置是非常有用的。