Tuesday, 22 November 2011

Regular Unit Tests and Stubs - Testing Techniques 4

My last blog was the third in a series of blogs on approaches to testing code and discussing what you do and don’t have to test. It’s based around my simple scenario of retrieving an address from a database using a very common pattern:


...and I proffered the idea that any class that doesn’t contain any logic doesn’t really need unit testing. In this I included my data access object, DAO, preferring instead to integration test this class to ensure it worked in collaboration with the database.

Today’s blog covers writing a regular or classical unit test that enforces test subject isolation using stub objects. The code we’ll be testing is, again, the AddressService:

@Component
public class AddressService {

 
private static final Logger logger = LoggerFactory.getLogger(AddressService.class);

 
private AddressDao addressDao;

 
/**
   * Given an id, retrieve an address. Apply phony business rules.
   *
   *
@param id
   *            The id of the address object.
   */
 
public Address findAddress(int id) {

   
logger.info("In Address Service with id: " + id);
    Address address = addressDao.findAddress
(id);

    address = businessMethod
(address);

    logger.info
("Leaving Address Service with id: " + id);
   
return address;
 
}

 
private Address businessMethod(Address address) {

   
logger.info("in business method");

   
// Apply the Special Case Pattern (See MartinFowler.com)
   
if (isNull(address)) {
     
address = Address.INVALID_ADDRESS;
   
}

   
// Do some jiggery-pokery here....

   
return address;
 
}

 
private boolean isNull(Object obj) {
   
return obj == null;
 
}

 
@Autowired
  @Qualifier
("addressDao")
 
void setAddressDao(AddressDao addressDao) {
   
this.addressDao = addressDao;
 
}
}

Michael Feather’s book Working Effectively with Legacy Code states that a test is not a unit test if:

  1. It talks to a database.
  2. It communicates across a network.
  3. It touches the file system.
  4. You have to do special things to your environment (such as editing configuration files) to run it.

To uphold these rules, you need to isolate your object under test from the rest of your system, and that’s where stub objects come in. Stub objects are objects that are injected into your object and are used to replace real objects in test situations. Martin Fowler defines stubs, in his essay Mocks Aren’t Stubs as:

“Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'”.

Picking a word to describe stubs is very difficult, I could choose dummy or fake, but there are types of replacement object that are known as dummies or fakes - also described by Martin Fowler:
  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
However, I have seen other definitions of the term fake object,for example Roy Osherove in is book The Art Of Unit Testing defines a fakes object as:
  • A fake is a generic term that can be used to describe either a stub or a mock object...because the both look like the real object.
...so I, like many others, tend to call all replacement objects either mocks or stubs as there is a difference between the two, but more on that later.

In testing the AddressService, we need to replace the real data access object with a stub data access object and in this case, it looks something like this:

public class StubAddressDao implements AddressDao {

 
private final Address address;

 
public StubAddressDao(Address address) {
   
this.address = address;
 
}

 
/**
   *
@see com.captaindebug.address.AddressDao#findAddress(int)
   */
 
@Override
 
public Address findAddress(int id) {
   
return address;
 
}
}

Note the simplicity of the stub code. It should be easily readable, maintainable and NOT contain any logic and need a unit test of its own. Once the stub code has been written, next follows the unit test:

public class ClassicAddressServiceWithStubTest {

 
private AddressService instance;

 
@Before
 
public void setUp() throws Exception {
   
/* Create the object to test */
    /* Setup data that's used by ALL tests in this class */
   
instance = new AddressService();
 
}

 
/**
   * Test method for
   *
{@link com.captaindebug.address.AddressService#findAddress(int)}.
   */
 
@Test
 
public void testFindAddressWithStub() {

   
/* Setup the test data - stuff that's specific to this test */
   
Address expectedAddress = new Address(1, "15 My Street", "My Town",
       
"POSTCODE", "My Country");
    instance.setAddressDao
(new StubAddressDao(expectedAddress));

   
/* Run the test */
   
Address result = instance.findAddress(1);

   
/* Assert the results */
   
assertEquals(expectedAddress.getId(), result.getId());
    assertEquals
(expectedAddress.getStreet(), result.getStreet());
    assertEquals
(expectedAddress.getTown(), result.getTown());
    assertEquals
(expectedAddress.getPostCode(), result.getPostCode());
    assertEquals
(expectedAddress.getCountry(), result.getCountry());
 
}

 
@After
 
public void tearDown() {
   
/*
     * Clear up to ensure all tests in the class are isolated from each
     * other.
     */
 
}
}

Note that in writing a unit test, we’re aiming for clarity. A mistake often made is to regard test code as inferior to production code with the result that it’s often messier and more illegible. Roy Osherove in The Art of Unit Testing puts forward the idea that test code should be more readable that production code. Clear tests should follow these basic, linear steps:
  1. Create the object under test. In the code above this is done in the setUp() method as I’m using the same object under test for all (one) tests.
  2. Setup the test. This is done in the test method testFindAddressWithStub() as the data used in a test is specific to that test.
  3. Run the Test
  4. Tear down the test. This ensures that tests are isolated from each other and can be run IN ANY ORDER.
Using a simplistic stub yields the two benefits of isolating the AddressService from the outside world and tests that run quickly.

How brittle is this kind of test? If your requirements change then the test and the stub changes - not so brittle after all?

As a comparison, my next blog re-writes this test using EasyMock.



A list of Blogs on Testing Techniques


1The source code is available from GitHub at:

git://github.com/roghughe/captaindebug.git

No comments: