I have below java class for which i have to write a junit test.
@Service
class My {
@Value("${emp.name}")
private String name;
public void printName() {
StringBuilder sb = new StringBuilder(name);
//do some stuff
}
}
Now I'm writing a test class for this java class that is like below.
@PrepareforTest({My.class})
public class MyTest {
@InjectMocks
My my;
@Test
public void printNameTest() {
//Test code
}
}
The problem here is i'm getting null pointer exception on StringBuilder sb = new StringBuilder(name); and test is breaking.
Anyone know how to load properties values in the test using @Value or pass the values to instance. I tried using reflection but it also gave error.