0

I'm having issues, while loading my spring boot application test properties.I have my application.properties file included under src/test/resources/ but when I was running test cases its failing because it was not able to load application context. Here is my test class looks like

@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());
}
}

Here is the stacktrace looks like:

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)
Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
TechGeek
  • 480
  • 1
  • 8
  • 22

1 Answers1

0

The stacktrace confirms that during test the creation of the application context is failing. It might be because of several reasons of which one might be , not able to read values from application.properties. There are a couple of issues that I noticed in your code.In your code you can remove the @ContextConfiguration annotation if you are using the @SpringBootTest annotation as per the documentation :

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.

  • You can provide you mock bean configurations for your test using the annotation @TestConfiguration instead.You should also exclude the normal configuration classes as this will create conflict if you are providing a separate test configuration for the same beans.
  • Also, if you have your application.properties with the test values under src/test/resources then there is no need for you to specify the path using @TestPropertySource as the file will be automatically taken up by springboot during test.
  • Also do not use the annotation @SpringBootTest and @RunWith(SpringRunner.class) togethor. These are both different annotations for different use cases. If you do not understand what to use when use the Link

Official doc

For further analysis correct your code then run again and if still the error persists please share the complete stacktrace .

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
  • I have updated the post with the new changes i have made. Now I'm getting null pointer exception. – TechGeek May 21 '20 at 12:10
  • @TechGeek you getting null pointer exception on `DeadLetterPropertiesTest.java:47` which is the `deadLetterProperties.getQueueName()` getter method . which means the getter is returning null. Could you share the code for the class so that we may analyze more. – Ananthapadmanabhan May 22 '20 at 03:45