当前位置: 首页 > 知识库问答 >
问题:

使用Spring启动执行器进行JUnit测试会出现异常

单于楚
2023-03-14

当我在Spring Boot应用程序中启用执行器时,我的JUnit测试失败并出现NullPointer异常。执行器工作正常,在我将执行器工件添加到pom.xml文件之前,测试工作正常。

好像是

@MockBean
MailSender mailSender;

是造成问题的原因

这是我的pom。xml文件:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

我的测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase
public class MailServiceTest extends CommonServiceTest {

    @Autowired
    MailService mailService;

    @MockBean
    MailSender mailSender;

    private static final String TO = "user@test.com";
    private static final String SUBJECT = "Subject";
    private static final String TEXT = "Text";
    private static final String TOKEN = "1234";

    @Value("${app.email.from}")
    private String fromEmail;

    @Value("${app.email.support}")
    private String supportEmail;

    private ArgumentCaptor<SimpleMailMessage> simpleMailMessageCaptor;

    @Override
    public void initObjects() {
        simpleMailMessageCaptor = ArgumentCaptor.forClass(SimpleMailMessage.class);
    }

    @Override
    public void initMocks() {

    }

    @Test
    public void sendMailTest() {

        mailService.sendMail(TO, SUBJECT, TEXT);

        verify(mailSender).send(simpleMailMessageCaptor.capture());

        assertEquals(TO, simpleMailMessageCaptor.getValue().getTo()[0]);
        assertEquals(SUBJECT, simpleMailMessageCaptor.getValue().getSubject());
        assertEquals(fromEmail, simpleMailMessageCaptor.getValue().getFrom());
        assertEquals(TEXT, simpleMailMessageCaptor.getValue().getText());
    }

}

错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailHealthIndicator' defined in class path resource [org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration$MailHealthIndicatorConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicator]: Factory method 'mailHealthIndicator' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1023)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 29 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicator]: Factory method 'mailHealthIndicator' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 46 more
Caused by: java.lang.NullPointerException
    at org.springframework.boot.actuate.autoconfigure.CompositeHealthIndicatorConfiguration.createHealthIndicator(CompositeHealthIndicatorConfiguration.java:42)
    at org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$MailHealthIndicatorConfiguration.mailHealthIndicator(HealthIndicatorAutoConfiguration.java:334)
    at org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$MailHealthIndicatorConfiguration$$EnhancerBySpringCGLIB$$25b5897.CGLIB$mailHealthIndicator$0(<generated>)
    at org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$MailHealthIndicatorConfiguration$$EnhancerBySpringCGLIB$$25b5897$$FastClassBySpringCGLIB$$da72ce26.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
    at org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$MailHealthIndicatorConfiguration$$EnhancerBySpringCGLIB$$25b5897.mailHealthIndicator(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 47 more

共有3个答案

湛功
2023-03-14

这通常是一些代理类包装<code>JavaMailSenderImpl<code>的症状。

检查您是否在依赖项中使用接口,例如JavaMailSender,您将看到HealthIndicatorAutoConfiguration.MailHealthIndicatorConfiguration查找并填充mailSender

public MailHealthIndicatorConfiguration(
        ObjectProvider<Map<String, JavaMailSenderImpl>> mailSenders) {
    this.mailSenders = mailSenders.getIfAvailable();
}
能烨华
2023-03-14

除了Leamas的建议之外,我想补充一点,如果您使用@MockBean来模拟mailssender,则可能会发生这种情况,在这种情况下,MailHealthIndicator似乎找不到MockedMailSender。我的情况就是这样,使用Leamas的建议来否定MailHealthIndicator是有效的。

司徒志
2023-03-14

我发现的唯一解决方案是在测试本身中添加一个属性:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase
// Otherwise I have a NullPointer, don't know why
@TestPropertySource(
        properties = {"management.health.mail.enabled=false"})
public class MailServiceTest extends CommonServiceTest {
 类似资料:
  • 从2.0.6开始使用spring boot starter test会带来JUnit 4依赖性。如何使用spring boot starter test(通过Gradle),但使用JUnit 5,而不引入JUnit 4依赖项? 如果有帮助,这是Gradle的部分依赖输出: 这是我的身材。gradle文件: 添加JUnit 5依赖项并执行注释中提到的排除就完成了。测试依赖项现在如下所示:

  • 我们用Rest Assured和JUnit 5编写了API测试,并试图并行运行测试,因为测试之间没有依赖关系。我们已经尝试了JUnit 5的实验性并行执行,方法是设置: …但我们已经看到,通常当测试失败时,测试报告中不包含它们的输出。 这是已知问题吗?有什么变通方法吗?

  • 问题内容: 到目前为止,我有两个测试。一个仅使用jUnit框架即可正常工作。另一个使用spring-test库并在每次尝试运行该异常时创建此异常。有什么想法可能导致问题吗? Error Maven test dependencies Dependency tree 问题答案: 你是否正在使用旧版本的Eclipse(Galileo或更低版本)?还是旧版的junit插件?如果是这样,则可能是问题的原因

  • 我还有一个其他相同的eclipse(相同的版本等等...它来自同一个.zip存档),在这里我可以为另一个项目运行相同类型的测试。我已经检查了配置,我没有发现任何差异。因此,我目前无法理解是什么导致了这个问题。 非常感谢你能帮我解决这个问题,

  • ...还有一个很简单的测试... 如果我在IntelliJ中运行这个,测试就会运行并失败。 如果我提交这个项目并将其推送到github,TeamCity会看到变化并开始构建。生成会很快失败,出现以下错误:

  • 我现在开始使用JUnit5和Spring Boot的测试。我有一个包含控制器、服务和存储库的Rest API,以及一些使用从我的获取属性的utils类。我没有使用Spring中的“profiles”,只是使用默认配置。 我的应用程序主: 我的项目的体系结构是: 我在主测试类中尝试了“@TestPropertySource”和/或“@ActiveProfiles(”Test“)”,但没有成功。还尝试