- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Junit5 为特定服务类创建单元/集成测试,以避免整个项目过载。
所以在这里,我尝试运行 EmailService
及其内部依赖类,但我得到 java.lang.IllegalStateException: Failed to load ApplicationContext。创建名称为“emailSenderService”的 bean 时出错。没有“org.springframework.mail.javamail.JavaMailSender”类型的合格 bean 可用:预计至少有 1 个符合 Autowiring 候选资格的 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 WARN 732 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailSenderService': Unsatisfied dependency expressed through constructor parameter 0; nested exception is 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: {} 2019-04-03 14:56:06.153 ERROR 732 --- [
main] o.s.b.d.LoggingFailureAnalysisReporter :*************************** APPLICATION FAILED TO START
Description:
Parameter 0 of constructor in com.server.server.service.EmailSenderService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.
2019-04-03 14:56:06.159 ERROR 732 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@342c38f8] to prepare test instance [com.server.server.test.junit.EmailServiceTest@4c7a078]
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ...
最佳答案
问题是您确实没有可用的JavaMailSender
(并且您在测试期间不需要真正的JavaMailSender)。您有四个选择:
在测试配置中注册模拟/ stub JavaMailSender
bean。
使用自动配置使您的 EmailSenderService
本身成为 @ConditionalOnBean(JavaMailSender.class)
并注册一个 stub (如果没有)(这通常是我如何测试“系统是否发送事务邮件?”)。
在这种情况下,由于您实际上是在尝试测试 EmailSenderService
本身,因此请在您的 应用程序中设置
属性(或注释)。spring.mail.host: localhost
-test
这里根本不要使用 Spring DI。构造函数注入(inject)的主要优点是您可以手动实例化您的 Bean,并且可以new
您的 EmailSenderService
及其依赖项。
如果我了解您测试的预期范围,#3 可能是适合您的解决方案。
关于java - Springboot测试在Junit 5中加载ApplicationContext失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55503490/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!