« April 2006 | Main | June 2007 »

May 17, 2007

Using classpath*: vs. classpath: when loading Spring resources.

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.

Posted by Alex Cruikshank at 10:59 AM | Comments (0)

May 15, 2007

DBUnit 2.2, Spring, and Testing

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

Posted by Christian Nelson at 9:30 AM | Comments (0)

May 6, 2007

Generic Custom Argument Matching in EasyMock

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.

Imagine we have a test fixture, BlogEntryController, which depends on a single service, BlogService. Because we want to focus our testing on the fixture, we'll inject a mock object as the service.

//follow EasyMock's convention of static imports
import static org.easymock.EasyMock.*
...
BlogEntryController controller = new BlogEntryController();
BlogService mockBlogService = createMock(BlogService.class);
controller.setBlogService(mockBlogService);

The next step is to record the expectations about the interactions between the fixture and its collaborator. In the record phase, we simply interact with the mock to record these expectations:

//record phase
BlogEntry blogEntry = new BlogEntry();
mockBlogService.saveBlogEntry(blogEntry);

//replay phase
replay(mockBlogService);

//method under test
controller.handleRequest(request, response);

//verify
verify(mockBlogService);

By default, argument expectations are verified using the equals() method. For the previous expectation, when the controller code is run, EasyMock will assert that the BlogEntry passed in by the controller equals() that passed in during the record phase. If, for instance, the controller creates its own BlogEntry and BlogEntry has the default implementation of equals(), this expectation would fail. Fortunately, EasyMock provides other argument matchers which can verify arguments based on different criteria:

mockBlogService.saveBlogEntry(isA(BlogEntry.class));

By using the isA(Class clazz) argument matcher, one of several predefined in EasyMock, the code will verify that the argument passed from the controller to the mock service is an instance of the BlogEntry class. By relaxing our expectations, we've come up with a test case which would pass. However, generally speaking, we'll probably want to test some more details about the BlogEntry. For instance, it's common to want to verify the argument's properties.

EasyMock has the facility to create custom argument matchers. It's a somewhat heavy weight solution requiring the implementation of 3 methods: 2 for the IArgumentMatcher interface and one static method used during the record phase. This facility seems better suited to creating reusable argument matchers (akin to the predefined ones which come with EasyMock) rather than singular matchers relevant only to a particular test case.

To bridge the gap, I have created a generic argument matcher, EasyMockUtils.argAssert(Assertion<E> assertion), which abstracts away the required EasyMock interaction code, allowing one to more simply express, inline if desired, the argument expectations:

static import common.test.easymock.EasyMockUtils;
...
mockBlogService.saveBlogEntry(argAssert(new Assertion<BlogEntry>() {
   void check(BlogEntry blogEntry) {
      assertEquals("submitted blog entry body", blogEntry.getBody());
      assertNull(blogEntry.getDateCreated());
    })
}

The argAssert static method takes a genericized instance of the Assertion interface in which one can declare any number of assertions against the argument passed in during the replay phase. As an added benefit, the Assertion implementation could also be leveraged to modify the passed in argument. For instance, imagine the controller depends on the service setting the dateCreated property on the passed in bean. We can ensure our mock replicates that behavior:

mockBlogService.saveBlogEntry(argAssert(new Assertion<BlogEntry>() {
   void check(BlogEntry blogEntry) {
      assertEquals("submitted blog entry body", blogEntry.getBody());
      assertNull(blogEntry.getDateCreated());
      //mimic the behavior of the true service
      blogEntry.setDateCreated(new Date());
    })
}

Mocking to this detail could trigger a warning regarding the level of coupling between the test, the fixture, and the collaborator; however, I have seen situations where it can make the difference between being able to use a mock object for testing or not. Mockist testers in general must be wary of unnecessarily deep coupling where, for instance, a refactor of the fixture/collaborator interactions could result in as much time spent updating test code even if the test's high level assertions remain unchanged. However, used attentively during test driven development, mock objects can help a programmer focus on keeping those interactions simple and clearly defined. Furthermore, testing with mock objects helps one to focus on unit testing the fixture at hand. In that situation, the generic custom argument matcher provides a simplified tool for expressing assertions about that code.

And here's the code. The generic interface which allows one to specify argument assertions:

Assertion.java

package common.test.easymock;

public interface Assertion<E>
{
   /**
    * Allows specifying varying assertions about an argument.
    * @param argument the argument being check
    * @throws Error throw an exception in the case of an assertion failure
    */
   void check(E argument) throws Error, Exception;
}

A simple container for the static method used to declare the argument matching assertion during the mock record phase.

EasyMockUtils.java

package common.test.easymock;

import org.easymock.EasyMock;

public class EasyMockUtils
{
   public static <E> E argAssert(Assertion<E> assertion)
   {
      EasyMock.reportMatcher(new ArgumentAssertion(assertion));
      return null;
    }
}

And the IArgumentMatcher implementation which ties the pieces together:

ArgumentAssertion.java

package common.test.easymock;

import org.easymock.IArgumentMatcher;

public class ArgumentAssertion implements IArgumentMatcher {
   private Assertion assertion;
   private Error assertionError;
 
   public ArgumentAssertion(Assertion assertion) {
      this.assertion = assertion;
    }
 
   public boolean matches(Object actual) {
      try
      {
         assertion.check(actual);
         return true;
       }
      catch (Error e)
      {
         assertionError = e;
         return false;
       }
      catch (Exception e)
      {
         assertionError = new Error(e);
         return false;
       }
    }
 
   public void appendTo(StringBuffer buffer) {
      buffer.append("argumentAssertion(exception ").append(assertionError).append(")");
    }
}

And of course a test:

ArgumentAssertionTest.java

package common.test.easymock;

import static common.test.easymock.EasyMockUtils.argAssert;
import junit.framework.TestCase;
import static org.easymock.EasyMock.*;

public class ArgumentAssertionTest extends TestCase
{
   public interface TestInterface
   {
      void testMethod(String arg);
    }
 
   TestInterface mock;
 
   protected void setUp() throws Exception
   {
      super.setUp();
      mock = createMock(TestInterface.class);
    }
 
   public void testArgumentAssertionSucess()
   {
      //record expected behavior
      mock.testMethod(argAssert(new Assertion<String>() {
         public void check(String argument)
         {
            //do nothing - should pass
          }
       }));
  
      //should get no errors
      replay(mock);
      mock.testMethod("test");
      verify(mock);
    }
 
   public void testArgumentAssertionFailure()
   {
      //record expected behavior
      mock.testMethod(argAssert(new Assertion<String>() {
         public void check(String argument)
         {
            fail("explicitely fail assertion");
          }
       }));
  
      replay(mock);
  
      boolean testSucceeded = false;
  
      try
      {
         mock.testMethod("test");
         verify(mock);
         testSucceeded = true;
       }
      catch (AssertionError e)
      {
         //should get this error
       }
  
      if (testSucceeded)
        fail("should have gotten a failure");
    }
}

Posted by Randy Puro at 12:05 PM | Comments (0)