When testing an stand alone spring environment we can do:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:spring-*.xml" })
@ActiveProfiles( profiles = { "Prod","bsi"})
public class SampleTest
When testing Struts 2 with spring integration we can use:
public class SessionManagement extends StrutsSpringTestCase {
@Override
public String[] getContextLocations() {
return new String[] {"classpath:spring-*.xml"};
}
}
But how can I set the active spring profile here?
You can set active profiles in spring via application context environment.
Since StrutsTestCase
uses GenericXmlContextLoader
which is not configurable you need to override setupBeforeInitDispatcher
method in your test and use some context (e.g. XmlWebApplicationContext
) where you can set profiles and call refresh
.
@Override
protected void setupBeforeInitDispatcher() throws Exception {
// only load beans from spring once
if (applicationContext == null) {
XmlWebApplicationContext webApplicationContext = new XmlWebApplicationContext();
webApplicationContext.setConfigLocations(getContextLocations());
webApplicationContext.getEnvironment().setActiveProfiles("Prod", "bsi");
webApplicationContext.refresh();
applicationContext = webApplicationContext;
}
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
applicationContext);
}
Since you're using JUnit 4 there is StrutsSpringJUnit4TestCase
abstract class. Extend it and you can use annotations just like in your SampleTest
.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:spring-*.xml" })
@ActiveProfiles(profiles = { "Prod","bsi"})
public class SessionManagement extends StrutsSpringJUnit4TestCase<SomeAction>