How to insulate yourself from static methods

Sometimes your code needs to call a static method in a 3rd party library and unit testing  suddenly becomes difficult, particularly when that static method requires context in order to work.

Enter the insulating class (also known as a Facade).

  1. Create a new interface that declares a method with the same signature as the static method you want to delegate to
  2. Create a new Facade class that implements the interface
  3. Replace the calls to the static method with calls to your new Facade class

Let’s assume that the method you need to call looks like this

[Read More]

How to be Agile when using 3rd party software framework

It can be difficult to be Agile when working with 3rd party software framework.  The vendor product may dictate many aspects of the software architecture thwarting your attempts at automated testing and continuous build.

However there are steps you can take to make Agile easier.  I’ll discuss a few of the options that have worked for me on my current project.

  • Insulate yourself from static methods.
  • Introduce wrappers (Facades) to vendor classes.
  • Introduce Spring, with different contexts for testing vs production, to allow easy substitution of vendor classes.
  • Make use of mock objects for testing your code in isolation.
  • If possible, move responsibility into services that live outside the framework.

In our particular case we’ve done all of the above and have managed to build a stable system with respectable test coverage.  We’ve also engaged the vendor to see if they can modify some of the framework interfaces to simplify unit testing, and they have been open to our suggestions.

[Read More]