Posts

Showing posts with the label spring-boot-test

Getting NullPointerException when tried to read @Autowired configuration object [duplicate]

Image
Getting NullPointerException when tried to read @Autowired configuration object [duplicate] This question already has an answer here: I am trying to read some properties from application.properties . I have created a configuration class with @Component and @ConfigurationProperties annotations. application.properties @Component @ConfigurationProperties When I am trying to access the configuration from a controller class, its working fine. But when I am trying to access the configuration from one component class, its throwing a null pointer exception. Following are the application.properties and classes. application.properties application.properties elasticsearch.ip=localhost InputManagementController.java @RestController public class InputManagementController { @Autowired private Configuration configuration; @GetMapping("/crawler/start") public String start(){ try{ System.out.println(configuration.getIp()); ----> getting value localhos...

Spring Boot Integration Tests override custom .properties file

Spring Boot Integration Tests override custom .properties file I am implementing integration tests and I have a problem with overriding custom properties that my application use. Integration test sample: @RunWith(SpringRunner.class) @SpringBootTest( classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT ) @TestPropertySource(locations = "classpath:test.properties") public abstract class BaseIntegrationTest { @Autowired protected TestRestTemplate restClient; The @TestPropertySource(locations = "classpath:test.properties") will override application.properties of my application with one that is provided @TestPropertySource(locations = "classpath:test.properties") application.properties However, my application use custom property file like this: @Configuration @PropertySource("classpath:fileProvider/custom.properties") @ConfigurationProperties(prefix = "com.sample.config") public class Sample...