...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:
- It talks to a database.
- It communicates across a network.
- It touches the file system.
- 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).
- 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.
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:
- 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.
- Setup the test. This is done in the test method testFindAddressWithStub() as the data used in a test is specific to that test.
- Run the Test
- Tear down the test. This ensures that tests are isolated from each other and can be run IN ANY ORDER.
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
- Testing Techniques - Part 1 - Not Writing Tests
- The Misuse of End To End Tests - Testing Techniques 2
- What Should you Unit Test? - Testing Techniques 3
- Regular Unit Tests and Stubs - Testing Techniques 4
- Unit Testing Using Mocks - Testing Techniques 5
- Creating Stubs for Legacy Code - Testing Techniques 6
- More on Creating Stubs for Legacy Code - Testing Techniques 7
- Why You Should Write Unit Tests - Testing Techniques 8
- Some Definitions - Testing Techniques 9
1The source code is available from GitHub at:
git://github.com/roghughe/captaindebug.git
No comments:
Post a comment