Archive for February, 2007

Unit testing interface implementations

Sunday, February 25th, 2007

It's bugged me for a while about how best to go about unit testing classes that implement multiple interfaces where those interfaces are implemented by multiple classes or where a class extends another class and implements one or more interfaces.

What I want to avoid is in each test class implementing the the same set of tests for the behavior specified by the interface. That's not very DRY.

If your classes only implement a single interface then you can have a base class with the tests for the interface and then separate test classes derived from that for each class implementing the interface. The problem arises when your classes implement multiple interfaces - Java doesn't support multiple inheritance for classes so you can't have a base test case for each interface.

One answer with JUnit 4 is to use the parameterized test runner - I haven't thought about JUnit 3, if you're not using JUnit 4 because you're still using a JVM that doesn't support annotations JSE 6 is out now, catch up!

You need to have a test case for your interface and configure this to run with the parameterized runner. You also have a static factory method that provides the various implementations of the interface to be used in each test.

As an example, say the container I'm writing (I'm not really - I know there is Apache Geronimo, JBoss, Hivemind etc - this is just an example) supports services that it can start and stop, so I have an interface Service and the various services that I have, e.g. caching, lookup, logging etc, implement this interface.

JAVA:
  1. /**
  2. * Represents a service that the container can run.
  3. *
  4. */
  5. public interface Service {
  6.  
  7.     /**
  8.       * Starts the service, may only be called if the service is not started.
  9.       *
  10.       * @throws IllegalStateException
  11.       *             if called when the service is already started.
  12.       */
  13.     public void start();
  14.  
  15.     /**
  16.       * Indicates whether the service is started
  17.       *
  18.       * @return true if the service is started, false otherwise
  19.       */
  20.     public boolean isStarted();
  21. }

I have a test case for Service that tests that the various implementations of Service adhere to the general contract. I might have the following three tests:

JAVA:
  1. public class ServiceTest {
  2.  
  3.     private Service service;
  4.  
  5.     @Test
  6.     public void shouldNotIndicatedStartedForNewObject() {
  7.         assertFalse(service.isStarted());
  8.     }
  9.  
  10.     @Test
  11.     public void shouldStartIfNotAlreadyStarted() {
  12.         service.start();
  13.         assertTrue(service.isStarted());
  14.     }
  15.  
  16.     @Test(expected = IllegalStateException.class)
  17.     public void shouldThrowExceptionIfStartedCalledOnAStartedService() {
  18.         service.start();
  19.         assertTrue(service.isStarted());
  20.         service.start();
  21.     }
  22. }

If I have a caching and lookup service and I want to test that both adhere to the contract of the Service interface then I have to annotate the ServiceTest to indicate that it should run with the parameterized runner and provide an annotated factory method that returns a collection of arguments to run the tests with.

So the full testcase is:

JAVA:
  1. @RunWith(Parameterized.class)
  2. public class ServiceTest {
  3.  
  4.     private Service service;
  5.  
  6.     public ServiceTest(final Service service) {
  7.         this.service = service;
  8.     }
  9.  
  10.     @Parameters
  11.     public static Collection implementations() {
  12.         return Arrays.asList(new Object[][] { { new CachingService() }, { new LookupService() } });
  13.     }
  14.  
  15.     @Test
  16.     public void shouldNotIndicatedStartedForNewObject() {
  17.         assertThat(service, is(not(started())));
  18.     }
  19.  
  20.     @Test
  21.     public void shouldStartIfNotAlreadyStarted() {
  22.         service.start();
  23.         assertThat(service, is(started()));
  24.     }
  25.  
  26.     @Test(expected = IllegalStateException.class)
  27.     public void shouldThrowExceptionIfStartedCalledOnAStartedService() {
  28.         service.start();
  29.         assertThat(service, is(started()));
  30.         service.start();
  31.     }
  32. }

The test classes for CachingService and LookupService now only need deal with testing the specifics of caching or lookups rather than testing adherence to the service specification.

I have yet to use this in production code on a large scale so we'll have to see how it works out, but it seems far better than having to replicate test code amongst several test cases.

Code syntax highlighting with indentation using iG:Syntax Highliter

Saturday, February 24th, 2007

Finally I've got iG:Syntax Hiliter to indent code in blog posts in WordPress. You need make sure you're not using the rich text editor, to do this there may be two options you need to uncheck: the "Users should use the visual editor by default" under Options/Writing and "Use the visual rich editor when writing" under Users/Your Profile.

What did we do before Google?