我在加载 Spring Boot 应用程序测试属性时遇到问题。我的 application.properties 文件包含在 src/test/resources/下,但是当我运行测试用例时,它失败了,因为它无法加载应用程序语境。这是我的测试类,如下所示
@SpringBootTest
@EmbeddedKafka(partitions = 1, controlledShutdown = true)
@Import(KafkaTestConfiguration.class)
@EnableAutoConfiguration(exclude = IntegrationAutoConfiguration.class)
public class DeadLetterPropertiesTest {
@Autowired
DeadLetterProperties deadLetterProperties;
@Value("errorqueue")
private String deadLetterQueueName;
@Test
public void queueNameTest() {
assertEquals(deadLetterQueueName,deadLetterProperties.getQueueName());
}
}
这是堆栈跟踪:
java.lang.NullPointerException at
com.test.DeadLetterPropertiesTest.queueNameTest(DeadLetterPropertiesTest.java:47) 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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 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.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
最佳答案
堆栈跟踪确认在测试期间应用程序上下文的创建失败。可能是由于多种原因导致无法从 application.properties
读取值。我在您的代码中注意到了几个问题。如果您按照文档使用 @SpringBootTest
注释,则可以在代码中删除 @ContextConfiguration
注释:
Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication.
- 您可以使用以下方法为您的测试提供模拟 bean 配置:
注释
@TestConfiguration
相反。您还应该排除 正常的配置类,因为如果您是这样,这会产生冲突 为相同的 bean 提供单独的测试配置。 - 此外,如果您的
application.properties
包含测试值 在src/test/resources
下,则无需指定 使用@TestPropertySource
作为文件的路径将是 测试时由springboot自动占用。 - 也不要同时使用注释
@SpringBootTest
和@RunWith(SpringRunner.class)
。这些都是针对不同用例的不同注释。如果您在使用Link时不明白该使用什么
官方doc
为了进一步分析,请更正您的代码,然后再次运行,如果错误仍然存在,请分享完整的堆栈跟踪。
关于java - Spring Boot 应用程序无法加载测试属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61926439/