jeudi 8 septembre 2016

Resolve profile based properties into spring tests

Into my spring RootConfig class I use properties file based on my spring profile:

@Configuration
@PropertySource("classpath:properties/app.properties")
@PropertySource("classpath:properties/app-${spring.profiles.active}.properties")
@ComponentScan(...)
public class RootConfig {   

    @Bean // just because you will ask about it
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

Now I want to write test class that use such configuration:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RootConfig.class)
public class RootConfigTest {

    @Test
    public void testContext() throws Exception {
        assertTrue(true);
    }

}

But my context is failed to start: java.lang.IllegalStateException: Failed to load ApplicationContext because

Could not resolve placeholder 'spring.profiles.active' in string value "classpath:properties/app-${spring.profiles.active}.properties"

And this is web application, so originally my spring profile configured at:

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.setInitParameter("spring.profiles.active", "dev");
    }

}

Aucun commentaire:

Enregistrer un commentaire