I am absolutely new to TestNG, Spring framework etc. and I'm trying to use the annotation @Value
access to configuration file via the @Configuration
annotation.
All I'm trying to achieve here is to make the console write out "hi" from the config file accessing the value via @Value
. I must be obviously missing the whole point of the @Value
annotation (or @Autowired
or some other annotations) as all I'm gettting is java.lang.NullPointerException
.
I have the following three files (reduced to the absolute minimum):
config.properties
a="hi"
TestConfiguration.java
@Configuration
@PropertySource("config.properties")
public class TestConfiguration {
@Value("${a}")
public String A;
}
TrialTest.java
public class TrialTest {
@Autowired
private TestConfiguration testConfiguration;
@Test
public void test() {
System.out.println(testConfiguration.A);
}
}
Thanks a lot.
Try annotate your test class with these:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={TestConfiguration.class})
[Edit] sorry I didn't see that OP was using TestNG. The essential point is still that the problem is caused by Spring not being bootstrapped. In TestNG that can be done via extending AbstractTestNGSpringContextTests
.