Enterprise Java

The Misuse of End To End Tests – Testing Techniques 2

My last blog was the first in a series of blogs on approaches to testing code, outlining a simple scenario of retrieving an address from a database using a very common pattern:

…and describing a very common testing technique: not writing tests and doing everything manually.

Today’s blog covers another practise which I also feel is sub-optimal. In this scenario, the developers use JUnit to write tests, but write them after they have completed writing the code and without any class isolation. This is really an ‘End to End’ (aka Integration) test posing as a Unit Test.

Although yesterday I said that I’m only testing the AddressService class, a test using this technique starts by loading the database with some test data and then grabbing hold of the AddressController to call the method under test. The AddressController calls the AddressService which then calls the AddressDao to obtain and return the requested data.

public class EndToEndAddressServiceTest {

  private AddressController instance;

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

    final int id = 10;
    BindingAwareModelMap model = new BindingAwareModelMap();

    String result = instance.findAddress(id, model);
    assertEquals("address-display", result);

    Address resultAddress = (Address) model.get("address");
    assertEquals(Address.INVALID_ADDRESS, resultAddress);

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

    final int id = 1;
    Address expected = new Address(id, "15 My Street", "My Town",
        "POSTCODE", "My Country");

    BindingAwareModelMap model = new BindingAwareModelMap();

    String result = instance.findAddress(id, model);
    assertEquals("address-display", result);

    Address resultAddress = (Address) model.get("address");
    assertEquals(expected.getId(), resultAddress.getId());
    assertEquals(expected.getStreet(), resultAddress.getStreet());
    assertEquals(expected.getTown(), resultAddress.getTown());
    assertEquals(expected.getPostCode(), resultAddress.getPostCode());
    assertEquals(expected.getCountry(), resultAddress.getCountry());

The code above uses Unitils to both load the test data into a database and to load the classes in our Spring context. I find Untils a useful tool that takes the hard work out of writing tests like this and having to setup such a large scale test is hard work.

This kind of test has to be written after the code has been completed; it’s NOT Test Driven Development (which from previous blogs, you’ll gather I’m a big fan), and it’s not a unit test. One of the problems with writing a test after the code is that developers who have to do it see it as a chore rather than as part of development, which means that it’s often rushed and not done in the neatest of coding styles.

You’ll also need a certain amount of infrastructure to code using this technique, as a database needs setting up, which may or may not be on your local machine and consequently you may have to be connected to a network to run the test. Test data is either held in test files (as in this case), which are loaded into the database when the test runs, or held permanently in the database. If a requirement change forces a change in the test, then the database files will usually need updating together with the test code, which forces you to update the test in at least two places.

Another big problem with this kind of test, apart from the lack of test subject isolation, is that fact that they can be very slow, sometimes taking seconds to execute. Shane Warden in his book ‘The Art of Agile Development’ states that unit tests should run at a rate of “hundreds per second”. Warden also goes on to cite Michael Feather’s book Working Effectively with Legacy Code for a good definition of what a unit test, or is not:

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.

…now I like that.

…although I don’t necessarily agree with point three. One of the main tenants of good unit test code is Readability. Method arguments that are passed to objects under test are sometimes large in size, especially when they’re XML. In this case I think that it’s more pragmatic to favour test readability and store data of this size in a data file rather than having it as a private static final String, so I only adhere to point three where ever practical.

Unit tests can be summed up using the FIRST acronym: Fast, Independent, Repeatable, Self Validating and Timely, whilst Roy Osherove in his book The Art Of Unit Testing sums up a good unit test as: “an automated piece of code that invokes the method or class being tested and then checks some assumptions about the logical behaviour of that method or class. A unit test is almost always written using a unit testing framework. It can be written easily and runs quickly. It’s full automated, trustworthy, readable and maintainable”.

The benefit of an End to End test is that they do test your test subject in collaboration with other objects and surroundings, something that you really must before shipping your code. This means that when complete, you code should comprise of hundreds of unit tests, but only tens of ‘End to End’ tests.

Given this, then my introductory premise, when I said that is technique is ‘sub-optimal’, is not strictly true; there’s nothing wrong with ‘End to End’ tests, every project should have some together with some ordinary integration tests, but these kinds of tests shouldn’t replace, or be called unit tests, which is often the case.

Having defined what a unit test is, my next blog investigates what you should test and why…

Reference: The Misuse of End To End Tests – Testing Techniques 2 from our JCG partner at the Captain Debug blog

Related Articles :

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button