gpt4 book ai didi

java - Springboot测试在Junit 5中加载ApplicationContext失败

转载 作者:行者123 更新时间:2023-11-30 05:41:45 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com