I am getting null while reading the values from .properties file when i am executing the test case. here while debugging the test case i am able to see the values which are loaded from properties file when the curser is there in the test class but when the curser enters into the actual class in that class i am getting the same values as null. And my code is as follows
Thanks in advance
@RestController
@PropertySource("classpath:/com/example/prop.properties")
public class ReadProp {
@Value("${name}")
private String name;
@Value("${rollNo}")
private String rollNo;
@RequestMapping(value="/")
public void getDetails(){
System.out.println(name);
System.out.println(rollNo);
}
}
and the test case is as follows
@RunWith(SpringRunner.class)
@SpringBootTest
@PropertySource("classpath:/com/example/prop.properties")
public class ReadPropTest {
private ReadProp readProp = new ReadProp();
@Value("${name}")
private String name;
@Value("${rollNo}")
private String rollNo;
@Test
public void readValues() {
System.out.println(name);
System.out.println(rollNo);
readProp.getDetails();
}
}
Instead of creating a new object using new ReadProp()
. You should Autowire it.
@Autowired
ReadProp readProp;
in your test class. If you create an object using new, you don't get the bean which spring created with all the value assigned using @Value
.