我正在尝试使用Junit5为特定服务类创建单元/集成测试,以避免整个项目过载。
所以在这里我尝试运行EmailService
及其依赖类,但我得到了java.lang.IllegalStateException: Failed to load ApplicationContext. Error creating bean with name 'emailSenderService. No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate.'
。
我必须运行整个应用程序来测试单个服务吗?
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","[email protected]","test message");
mailService.sendUserContactEmail(contact);
Assertions.assertTrue(smtp.waitForIncomingEmail(1));
}
}
错误:
2019-04-03 14:56:06.146 WARN 732 --- [main] oswcsGenericWebApplicationContext:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'emailSenderService'的bean时出错:通过构造函数参数0表示的不满意的依赖性;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型'org.springframework.mail.javamail.JavaMailSender'的限定bean可用:预计至少有1个bean有资格作为autowire候选者。依赖注释:{} 2019-04-03 14:56:06.153 ERROR 732 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter:
***************************申请失败
描述:
com.server.server.service.EmailSenderService中构造函数的参数0需要一个无法找到的类型为'org.springframework.mail.javamail.JavaMailSender'的bean。
行动:
考虑在配置中定义类型为'org.springframework.mail.javamail.JavaMailSender'的bean。
2019-04-03 14:56:06.159 ERROR 732 --- [main] ostest.context.TestContextManager:允许TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@342c38f8]准备测试实例时发生异常[ com.server.server.test.junit.EmailServiceTest@4c7a078]
java.lang.IllegalStateException:无法在org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext上加载ApplicationContext(DefaultCacheAwareContextLoaderDelegate.java:125)~ [spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE在org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)...
问题是你真的没有可用的JavaMailSender
(你在测试期间不需要真正的JavaMailSender
)。您有四种选择:
EmailSenderService
bean。@ConditionalOnBean(JavaMailSender.class)
本身EmailSenderService
并注册一个存根(如果没有)(这通常是我做的测试“系统是否发送交易邮件?”)。spring.mail.host: localhost
本身,请在application-test
属性(或注释)中设置new
。EmailSenderService
你的qazxswpoi及其依赖项。如果我理解您的测试的预期范围,#3可能是适合您的解决方案。