Posts Tagged ‘test’

The Importance of Being Testable

October 31, 2010

Designing and building for testability in software at all levels (functional and unit testing) is one of those things most people don’t get. When I first started out in software I remember getting stuck with a last minute task of creating a web client for a test team who needed to test a XML web service. At the time I cursed the task and didn’t really think there was much value in creating the test client. Now though I try and put testing hooks like that all over the place. After looking back at software systems I have designed or worked on, I have realized that one of the main things that people have cursed or praised code on is testability.

Why Testability is Important?

Every developer has their own style for design and coding and some of them are going to think anything else is basically wrong, even if it works. So trying to win people over with coding style or awesome design is a losing battle. The central concept to all good code and systems though regardless of style or design is being able to validate if it works (aka testability). The more you can isolate each part of the system into individual testable parts the easier it becomes for people to feel confident in making changes. Which is ultimately what it’s all about. If someone can come into a system they have never worked on before, make a change, and feel confident about the change, they can’t get too upset.

Designing for Testability

Testability is all about isolation. If you are creating a system with dependencies on another system you need to be able to eliminate the need for that other system for testing. There will be scenarios where you don’t want to have the dependency whether it’s during a unit test or you don’t have access to that system in certain testing environments. Solutions for this could involve the ability to configure different end points at run time and/or putting proxies in front of calls to intercept and return mock responses. The same goes for code, you need to be able to isolate units of code and make them fail or pass tests only if that code is broken. This will involve using factory patterns, dependency injection, and/or other loose coupling strategies.

All Systems Must be End User Testable

Even if your developing a service which operates over an obscure protocol with proprietary message format, there is no excuse for not having a test interface available to allow non-developers to test the functionality. In many cases this means creating a test program or test interface on a system. This may not be in the requirements however it should be. It’s left out many times because they are written in the context of an integrating system which drives testing. Unfortunately the integrating system may or may not be available in all environments and also there could be bugs in the integrating system requiring alternate testing interfaces to validate your system which should validate the need for a end user test interfaces. One of the main places you’ll find integrated systems missing is working locally as a developer. All these aspects of testing make it desirable to get end user testability in what you are developing and it will help people down the line as well.


Remember the Abstract Factory Pattern

May 6, 2010

With all the emphasis these days on unit testing it amazes me that I see developers are still dropping bombs like…

  1 public void someMethodThatMyUnitTestHasToCall(){
  2        ... Method Logic ...
  3        SuperComplexObject object = new SuperComplexObject();
  4        object.doSomethingThatIDesperatelyWantToAvoidForMyTest();
  5        // or an alternatively horrific call
  6        DifferentClass.staticMethodCallWhichCallsSomethingElseIWantToAvoid();
  7 }

Nothing kills a unit test faster than near un-mockable behavior in the middle of a method call which you can’t avoid (at least in java). ┬áTypes of actions taken in these undesirable method calls and object creations are generally database activity or some type of HTTP access when cause you to write more code to setup a unit test than the code under test itself. Many cases can be refactored with simply making the object an instance variable, however when an object has a lot of state which shouldn’t be retained from call to call thats not an option. I want to remind folks in these cases of the Abstract Factory Pattern which is a very straight forward way to wrap these monsters up. Sometimes people may avoid the exact pattern because it involves creating an extra class and interface in its pure form which sometimes is undesirable especially if you have a lot of classes which need to be hidden, ┬áso I’ll offer a variation on the pattern which hopefully will make it more palatable.

Modified Abstract Factory

The modified abstract factory I’ll suggest is simply a single class that encapsulates the unwanted call. For Object creation it will always be the actual “new Object()” statement because if you can’t get another instance type created your out of luck. In the case of static method calls its the method call itself.

  1 public class ComplexObjectFactory {
  2    public ComplexObject getComplexObject(){
  3        ComplexObject object = new ComplexObject();
  4        return object;
  5    }
  6 }

Injecting the Factory

Now we need to get the factory into the class somehow there are a couple of ways to do this…

  • Pass a factory instance in as new parameter
  • Create a settable field in the class for the new factory

Depending on the situation you could have a other options as well but the one option that isn’t there is just replacing “new Object()” with “new ObjectFactory()” that won’t get you any closer to solving the problem with the suggested solution I’m providing. Now you can see how the old method changes.

  1 ComplexObjectFactory factory = new ComplexObjectFactory();
  3 public void methodCalledByUnitTest(){
  4    ComplexObject object = factory.getComplexObject();
  5    object.complexMethodCall();
  6 }
  8 public void setComplexObjectFactory(ComplexObjectFactory factory) {
  9     this.factory = factory;
 10 }

Unit Test Mocking

Now there is a hook to where you can create an extension of the quasi abstract factory class which returns a mock instance of your class or returns mocked data for a static method call.

  1 ComplexObjectFactory factory = new ComplexObjectFactory() {
  2      public ComplexObject getComplexObject() {
  3           //Mock up the behavior in someway.
  4           return new MockComplextObject():
  5      }
  6 };

All you need to do is set the new factory implementation. This will allow your unit test to focus on the code under test not the code that was tangentially called by the code under test (assuming thats your testing methodology).