When i run my junit for Rest Controller class, properties in Rest controller is throwing Nullpointer exception. Below is my sample code. when i run the testFileStatus() method of TestingControllerTest calass, i am getting Nullpointer exception. Kindly help to resolve the issue. Any suggestion why i can't able to access the property even though it is available in test application.properties.
@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class TestingController {
@Value("${file.dir.status}")
private String fileDirectoryStatus;
@GetMapping(value="/filedetails")
public String getFileDetails(.....){
if(fileDirectoryStatus.equalsIgnoreCase("true")) { //NullpointerException in Junit test
//code to process file
}
}
}
src/main/resources/application.properties:
file.dir.status=false
src/test/resources/application.properties:
file.dir.status=false
@SpringBootTest
@TestPropertySource(
locations = "classpath:resources/application.properties")
public class TestingControllerTest{
@InjectMocks
private TestingController testingController;
@Test
public void testFileStatus() {
String status=testingController.getFileDetails(...);
}
}