I recently resolved a build problem nestled deep within the esoterica of spring resource loading. The behavior of classpath: URLs is explained at length in the Spring documentation, but, sadly, this is probably the last place a developer of my disposition would look. The problem (which I describe in more detail beyond the fold) was that we were unable to load hibernate mapping files from our Spring configured integration tests even though they gave us no trouble in our web application. The solution was to replace the “classpath:” prefix of our mappingLocation URI with “classpath*:”.
In this project, we are storing our Hibernate mapping files as separate files located in the package of the class they map (i.e. “Foo.hbm.xml” stored in “src/main/java/com/example/”). We inherited the persisted classes and Hibernate and Spring configuration from another project. The sessionFactory configuration in spring specified the mappingLocations as “classpath:**/*.hbm.xml” which worked fine when we deployed the web application.
We soon ran into problems when we added integration tests to test new DAO functionality. The test configuration was not able to find any hibernate mappings even though we were using the same “classpath:**/*.hbm.xml” URI. After a few hours of hair-pulling and a deep tour of the Spring source, we realized that the problem was that our test Spring configuration was in a different classpath root from the Hibernate mappings, even though both directories were in the test’s classpath.
When you use ‘classpath:’ for an Ant style wildcard search, Spring uses a single classpath directory for the search. The documentation is vague, but it seems the directory returned will be the first one provided by ClassLoader.getResources(“”). In our case, it returned the ‘/target/test-classes’ directory that contains applicationTest.xml and our test classes, instead of ‘/target/classes’ which contains application.xml and all the *.hbm.xml files.
Using the ‘classpath*:’ prefix fixes the problem. It indicates that the resource loader should look in all directories on the classpath, so making this change solved our problem. Apparently Spring maintains both prefixes because limitations in the Classloader (at the specification level) make it difficult to search for resources in the classpath root when performing wildcard searches across all classpath directories. This suggest it might be good practice to always create a directory to contain resources you might otherwise want to put in the classpath root and always use the ‘claspath*:’ prefix.
You may have noticed that DBUnit changed its connection closing behavior in v2.2. We noticed it when tests deriving from our custom DatabaseTestCase implementation (which uses DBUnit) starting failing. Initially we just reverted back to version 2.1. Since then I’ve discovered what the problem was and found a workaround, and while I was at it, I created some nice utilities to be used in tests which need data fixtures.
Problem
In v2.2 DBUnit introduces some new abstractions, one of which is the IDatabaseTester. Whether it’s by design or by accident, the default implementation closes connections after executing its operations (more info). The end result is that in our unit tests, the database connection is closed before the test can run.
Solution
I’ve created two simple util classes for loading test data into a database without closing the connection:
/** A helper for loading data sets into unit tests. */
public class DatabaseUtils {
public static void loadDataSet(Class clazz, final DataSource dataSource) throws Exception {
IDataSet dataSet = new FlatXmlDataSet(TestUtils.datasetInputStream(clazz));
IDatabaseTester tester = new ExistingConnectionDatabaseTester(dataSource);
tester.setDataSet(dataSet);
tester.onSetup();
}
}
/** A special DatabaseTester that doesn’t close the connection when its done. */
public class ExistingConnectionDatabaseTester extends AbstractDatabaseTester {
private DataSource dataSource;
public ExistingConnectionDatabaseTester(DataSource dataSource) {
super();
this.dataSource = dataSource;
}
public IDatabaseConnection getConnection() throws Exception {
return new DatabaseConnection(DataSourceUtils.getConnection(dataSource));
}
public void closeConnection(IDatabaseConnection connection) throws Exception {
// Don't close that connection!
}
}
The first of these depends on another test-related utility, TestUtils.java (see below), which converts a class to a path. The second depends on Spring’s DataSourceUtils.
/**
* A set of utilities that generate paths from classnames. This is useful when
* making reference to resources used by test cases, when the resources are
* located in a directory path matching the class' package.
*/
public class TestUtils {
public static final String TEST_PREFIX = "src/test/resources/"; // Maven2 default
public static String pathString(Class clazz) {
return pathString(TEST_PREFIX, clazz, "");
}
public static String pathString(Class clazz, String resource) {
return pathString(TEST_PREFIX, clazz, resource);
}
public static String pathString(String prefix, Class clazz, String resource) {
prefix = (prefix != null ? prefix : "");
StringBuffer sb = new StringBuffer();
sb.append(prefix);
if (!prefix.endsWith("/")) {
sb.append("/");
}
sb.append(ClassUtils.classPackageAsResourcePath(clazz));
sb.append("/");
sb.append(resource);
return sb.toString();
}
public static InputStream pathInputStream(Class clazz, String resource)
throws FileNotFoundException {
return pathInputStream(TEST_PREFIX, clazz, resource);
}
public static InputStream pathInputStream(String prefix, Class clazz, String resource)
throws FileNotFoundException {
return new FileInputStream(pathString(prefix, clazz, resource));
}
public static InputStream datasetInputStream(Class clazz) throws FileNotFoundException {
return pathInputStream(TEST_PREFIX, clazz, ClassUtils.getShortName(clazz) + ".xml");
}
}
These three classes give us everything we need to load data into our test database using DBUnit. And TestUtils can be used to load other test resources from the classpath as well, like images, documents, CSVs, etc.
Here’s an example of how it can be used in a DAO test based on Spring’s test classes.
public class ArtistHibernateDaoTest extends AbstractAnnotationAwareTransactionalTests {
private ArtistHibernateDao artistDao;
public void setArtistDao(ArtistHibernateDao artistDao) {
this.artistDao = artistDao;
}
protected String[] getConfigLocations() {
return new String[]{"applicationContext-database.xml", "applicationContext-hibernate.xml"};
}
protected void onSetUpInTransaction() throws Exception {
DatabaseUtils.loadDataSet(getClass(), getJdbcTemplate().getDataSource());
}
public void testFindAllNames() {
List<String> names = artistDao.findAllNames();
assertEquals(3, names.size());
assertTrue(names.contains("The Cure"));
assertTrue(names.contains("Depeche Mode"));
assertTrue(names.contains("New Order"));
}
}
onSetUpInTransaction() loads the data set using the convention of (packagename).(ClassName).xml before each test (e.g. com.c5.PizzaTest loads the file com/c5/PizzaText.xml on the classpath). In this case the data is loaded in the same transaction as the test so that no clean-up is necessary when the test is completed. These tests run fast!
We’ve been talking about moving away from our custom DatabaseTestCase class hierarchy. The above functionality in conjunction with Spring’s test hierarchy could be a good start.
On a somewhat related note, there is a new unit testing framework called Unitils which does this sort of thing and much more. I haven’t used it but it sounds interesting.
Christian
EasyMock is a clean, simple library for creating mock objects. It provides a whole range of facilities for declaring our expectations of the method call interactions on the mock including call count, call order, return values, exceptions, and argument matchers. Unfortunately, the argument matchers provide a limited set of argument expectations and a somewhat cumbersome process for expanding that set. Using java generics, it is possible to create a custom argument matcher allowing simple expression of unlimited assertions about Object arguments.
Continue reading ‘Generic Custom Argument Matching in EasyMock’