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.

@RunWith(UnitilsJUnit4TestClassRunner.class)
@SpringApplicationContext("servlet-context.xml")
@Transactional(TransactionMode.DISABLED)
public class EndToEndAddressServiceTest {

  @SpringBeanByType
  private AddressController instance;

  /**
   * Test method for
   * {@link com.captaindebug.address.AddressService#findAddress(int)}.
   */
  @Test
  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)}.
   */
  @Test
  @DataSet("FindAddress.xml")
  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 :

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


6 − = zero



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books