I have a spring boot application. I am writing Junit tests. I am trying to inject values from application.properties (defined in src/main/resources/application.properties) and a Status bean configured in AppConfig (src/main/java/hello/AppConfig.java). I see that the bean is autowired (through debugger it is not null) but the values are not set.
Here is application.properties
src/main/resources/application.properties
app.name=rest-service
app.version=1.0.0-SNAPSHOT
src/main/java/hello/AppConfig.java
@Configuration
public class AppConfig {
@Value("${app.version}")
private String version;
@Value("${app.name}")
private String name;
@Bean
public Status status(){
return new Status(name,version);
}
}
//Status is a simple pojo with name and version fields
src/test/java/hello/GreetingControllerTests.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ContextConfiguration(classes={hello.TestConfig.class,hello.AppConfig.class})
@WebAppConfiguration
public class GreetingControllerTests {
@Autowired
Environment envi;
@Autowired
Status status;
@Value("${app.name:notConfigured}")
String appName;
private String env;
@Before
public void init(){
System.out.println(status.getName());
System.out.println(appName);
env=envi.getActiveProfiles()[0];
}
@Test
public void should_fail(){
if (env.equalsIgnoreCase("DEV")){
assertThat(false).isFalse();
}
if (env.equalsIgnoreCase("QA1")){
System.out.println("failing the test");
fail();
}
}
}
I set debugger in the init method and found that both appName
and status
bean with right values set are NOT injected. Status bean is injected though. just the values are not set.