I have a spring boot application. I am writing Junit tests. I am trying to enter values ββfrom application.properties (defined in src / main / resources / application.properties) and Status bean configured in AppConfig (src / main / java / hello / AppConfig.java). I see that the bean is autwired (through the debugger it is not null), but the values ββare not set.
Here is the application.properties
Src / core / resources / application.propertie s
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 install the debugger in the init method and find that both the appName and the status bean with the right value sets are NOT inserted. The bean status is entered. just the values ββare not set.
source share