For a real-life integration test it is generally more appropriate to load the context in the JUnit setUp() method and closed in the tearDown(), so that each test method does not have to repeat the step of invoking ApplicationContext get the the beans.
One concern is that while this will avoid code duplication, the context will still be created once for each test method, as calls to setup()/teardown() surround each test method invocation. JUnit does this to ensure fresh base test benefit, the base class is capable of triggering the direct injection of dependencies into the test case class, via setter injection(autowired by type), or field injection matching by name ,as below,
public class WeatherServiceTest extends
AbstractDependencyInjectionSpringContextTests {
private WeatherService weatherService;
public void setWeatherService(WeatherService weatherService) {
this.weatherService = weatherService;
}
protected String[] getConfigLocations() {
return new String[]{"ch03/sample3/applicationContext.xml"};
}
public void testWeatherService() throws Exception {
Double high = weatherService.getHistoricalHigh(
new GregorianCalendar(2004, 0, 1).getTime());
// ... do more validation of returned value here...
}
}
Comments
Post a Comment