Rather than always having my tests go to a database, I have my DAOs all mocked up.
For most of my tests, I want to use this mock layer (much faster). For some cases I want to use the full layer (e.g. to test the real DAOs, or for integration tests).
I wired up my BaseTestCase to handle both cases in a simple class, which ties into the Spring Dependency post from yesterday:
package foo.test;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public abstract class BaseTestCase extends AbstractDependencyInjectionSpringContextTests {
private static String[] realPaths = { "classpath:applicationContext*.xml" };
private static String[] mockPaths = { "classpath:test-applicationContext.xml", "classpath:applicationContext-webwork.xml" };
protected Log log = LogFactory.getLog(this.getClass().getName());
/**
* Override method to choose which layer to use
*
* Default to using the mock layer
*/
protected boolean useMockLayer() {
return true;
}
protected String[] getConfigLocations() {
return useMockLayer() ? mockPaths : realPaths;
}
}
Now, I just place my mocks in the test-applicationContext.xml:
<bean id="userDAO" class="foo.dao.MockUserDAO"/>
If I want a test to use the full layer I simply override:
protected boolean useMockLayer() {
return false;
}