Fluent Java APIs II: The MockBuilder Pattern

Context

  • In a TDD environment, the same coding standards should be enforced on Unit Test code as on application code. Often, however this isn’t the case! Remember: unit tests can contain technical debt too!
  • Mocking service calls with EasyMock involves repetitive code.
  • This repetitive code can be simplified, and rendered more fluent with the Builder pattern.

Benefits of a MockBuilder

  1.   Sharpened focus: unit tests becomes more focussed on the code under test , rather than code written to support or prepare the test.
  2. Cheaper to write more and more tests.

First Iteration: mocking a service call to the ClientService with EasyMock

Consider an example of a unit test where there is a call to the ClientService that we want to replace with a call to a Mock:

@Test
public void testLoadPresetFormWithClient() throws RemoteException {

    NewBusinessApplication nba = NewBusinessApplicationBuilder.create()
    .withPresetOwner()
    .build();

    nba.getOwner().setRegisteredAddress(null);
    nba.getOwner().setClientNumber("12345678");

    Client stubbedClient = new Client();
    stubbedClient.setAddress(new Address());
    stubbedClient.getAddress().setLine1("Client Address 1");
    stubbedClient.getAddress().setLine2("Client Address 2");
    stubbedClient.getAddress().setLine3("Client Address 3");
    stubbedClient.getAddress().setLine4("Client Address 4");
    stubbedClient.getAddress().setLine5("Client Address 5");

    ClientService mockClientService = EasyMock.createMock(ClientService.class);
    EasyMock.expect(mockClientService.findByClientNumber((String)EasyMock.anyObject())).andReturn(stubbedClient).anyTimes();
    EasyMock.replay(mockClientService);
    EasyMock.verify(mockClientService);

    ClientServiceDelegate.getInstance().setClientService(mockClientService);

    EmployerCorrespondenceDetailsForm form = service.loadPresetForm(nba);

    assertFormAndClientRegisteredAddressEquals(form,mockClientService.findByClientNumber("12345678"));

    assertFormAndApplicationBusinessAddressEquals(form,nba);
}

The vast majority of the code in this unit test is support code, written to mock the client service call, and ensure a stubbed client is used for the purposes of the test.

In fact , only the last three lines of this test method are actual test code, executing the method under test, and evaluating the results

Second Iteration: Create ClientServiceMockBuilder

All the semi-boilerplate EasyMock code related to mocking the client service is now moved to it’s own object, where the builder pattern is implemented:

public class ClientServiceMockBuilder {

    private ClientService mockClientService;

    public static ClientServiceMockBuilder create() {
        return new ClientServiceMockBuilder();
    }

    public ClientService build() {
        EasyMock.replay(mockClientService);
        EasyMock.verify(mockClientService);
        return mockClientService;
    }

    private ClientServiceMockBuilder() {
        mockClientService = EasyMock.createMock(ClientService.class);
    }

    public ClientServiceMockBuilder withFindByClientNumberStubbedClient(){
        try{
            EasyMock.expect(mockClientService.findByClientNumber((String)EasyMock.anyObject())).andReturn(createStubbedClient()).anyTimes();
        }   catch(RemoteException e){
            throw new RuntimeException(e);
        }
        return this;
    }

    private Client createStubbedClient(){
        Client client = new Client();
        client.setAddress(new Address());
        client.getAddress().setLine1("Client Address 1");
        client.getAddress().setLine2("Client Address 2");
        client.getAddress().setLine3("Client Address 3");
        client.getAddress().setLine4("Client Address 4");
        client.getAddress().setLine5("Client Address 5");
        return client;
    }

}

Revisiting the Unit Test, it is now a lot cleaner, and more readable. More to the point though, we are pointing the way to any unit test that needs to mock a call to the Client Service:

@Test
public void testLoadPresetForm_WithClient() throws RemoteException {

    NewBusinessApplication nba = NewBusinessApplicationBuilder.create()
     .withPresetOwner()
     .build();

    nba.getOwner().setRegisteredAddress(null);
    nba.getOwner().setClientNumber("12345678");

    ClientService mockClientService = ClientServiceMockBuilder.create()
     .withFindByClientNumberStubbedClient()
     .build();

    ClientServiceDelegate.getInstance().setClientService(mockClientService);

    EmployerCorrespondenceDetailsForm form = service.loadPresetForm(nba);

    assertFormAndClientRegisteredAddressEquals(form,mockClientService.findByClientNumber("12345678"));

    assertFormAndApplicationBusinessAddressEquals(form,nba);
}

Third Iteration: Abstract out common MockBuilder behaviour

If your unit test code involves the use of many different mocked objects, you’ll be building a lot of MockBuilder objects to correspond with these. You’ll also quickly notice repetition of certain code, which can be abstracted to an abstract parent class called MockBuilder, as below:

public abstract class MockBuilder<T extends Object> {

    protected T mockedObject;

    public T build() {
        EasyMock.replay(mockedObject);
        EasyMock.verify(mockedObject);
        return mockedObject;
    }

}

The updated ClientServiceMockBuilder now looks like this:

public class ClientServiceMockBuilder extends MockBuilder<ClientService> {

    public static ClientServiceMockBuilder create() {
        return new ClientServiceMockBuilder();
    }

    private ClientServiceMockBuilder() {
        mockedObject = EasyMock.createMock(ClientService.class);
    }

    public ClientServiceMockBuilder withFindByClientNumberStubbedClient(){
        try{
            EasyMock.expect(mockedObject.findByClientNumber((String)EasyMock.anyObject())).andReturn(createStubbedClient()).anyTimes();
        }   catch(RemoteException e){
            throw new RuntimeException(e);
        }
        return this;
    }

    private Client createStubbedClient(){
        Client client = new Client();
        client.setAddress(new Address());
        client.getAddress().setLine1("Client Address 1");
        client.getAddress().setLine2("Client Address 2");
        client.getAddress().setLine3("Client Address 3");
        client.getAddress().setLine4("Client Address 4");
        client.getAddress().setLine5("Client Address 5");
        return client;
    }

}

And we can extend the MockBuilder again as required. For example, here’s another implementation, a Spring MVC BindingResultMockBuilder

public class BindingResultMockBuilder extends MockBuilder<BindingResult> {

    public BindingResultMockBuilder() {
        mockedObject = EasyMock.createMock(BindingResult.class);
    }

    public static BindingResultMockBuilder create() {
        return new BindingResultMockBuilder();
    }

    public BindingResultMockBuilder withErrors(boolean hasErrors) {
        EasyMock.expect(mockedObject.hasErrors()).andReturn(hasErrors).anyTimes();
        return this;
    }

    public BindingResultMockBuilder withAddAllErrors() {
        mockedObject.addAllErrors((Errors) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        return this;
    }

    public BindingResultMockBuilder withGetAllFieldErrors() {
        EasyMock.expect(mockedObject.getFieldErrors()).andReturn(new ArrayList<FieldError>()).anyTimes();
        return this;
    }

    public BindingResultMockBuilder withRejectValue() {
        mockedObject.rejectValue((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        return this;
    }
}

Results

  1. The MockBuilder can be extended to create a new subclass for anything you need to mock.
  2. Large amounts of boilerplate code can be refactored into a simpler invocation of a Builder.
  3. Cleaner Unit Tests = Better Unit Tests.

Fluent Java APIs I: The Builder Pattern

On February 23rd, PaddyPower hosted their inaugural technology series event, anchored around an excellent presentation on Java coding practices given by John Ferguson Smart – a Java and TDD expert. Smart’s presentation focussed on specific Java best practices, with the noble goal of more readable, more maintainable and more testable code.

A number of techniques that Smart was pushing were in the area of Fluent APIs. I’ve been using a few of them since the talk. In this blog post – and maybe one or two more – I’ll describe some of them in detail. I’ll be using a sandbox Github project to code up these examples, so you can access the code via Github here (with Git or SVN).

Right, let’s start with an initial application of the Builder pattern.


The Builder Pattern

Consider the following Contract object, a reasonable facsimile of a contract object you might see in a Life Assurance system:


public class Contract {

 private String contractNumber;
 private Date commencementDate;

 private Product product;

 private ContractStatus contractStatus;

 private Client[] contractOwners;
 private Client[] livesAssured;

 private BillingMethod billingMethod;

 public Contract() {
 }

 public Contract(String contractNumber, Date commencementDate, Product product, ContractStatus contractStatus, Client[] owners, Client[] livesAssured, BillingMethod billingMethod) {

     this.contractNumber = contractNumber;
     this.commencementDate = commencementDate;
     this.product = product;
     this.contractStatus = contractStatus;
     this.contractOwners = owners;
     this.livesAssured = livesAssured;
     this.billingMethod = billingMethod;
 }
 ...
}

The traditional way to construct this object could be as with the below, long method:

public class NewContractController {

 public Contract createNewContract(NewContractForm form) {

     Contract contract = new Contract();
     contract.setContractNumber(form.getContractNumber());
     contract.setCommencementDate(form.getCommencementDate());
     contract.setContractOwners(new Client[]{form.getFirstOwner(), form.getSecondOwner()});
     contract.setLivesAssured(new Client[]{form.getFirstLifeAssured(), form.getSecondLifeAssured()});
     contract.setProduct(form.getProduct());
     BillingMethodTypes thisBillingMethodType = Enum.valueOf(BillingMethodTypes.class, form.getBillingMethod());
     BillingMethod billingMethod = new BillingMethod(thisBillingMethodType);
     contract.setBillingMethod(billingMethod);

     return contract;
 }
...
}

The above code is fit for purpose, but it certainly isn’t easy to read. Here’s how it looks after a first sweep of refactoring:

public Contract secondCreateNewContract(NewContractForm form) {

  Contract contract = new Contract();
  contract.setContractNumber(form.getContractNumber());
  contract.setCommencementDate(form.getCommencementDate());
  contract.setContractOwners(getOwners(form));
  contract.setLivesAssured(getLivesAssured(form));
  contract.setProduct(form.getProduct());

  BillingMethod billingMethod = BillingMethodBuilder.create()
     .withType(form.getBillingMethod()).build();
  contract.setBillingMethod(billingMethod);

  ContractStatus contractStatus = ContractStatusBuilder.create()
    .withType(form.getContractStatus()).build();
  contract.setContractStatus(contractStatus);

  return contract;
 }

This new version is an improvement. We’re getting the owners and lives assured in separate methods, and we’ve introduced a couple of builders,for billing method and contract status.

Let’s look at just one of those builders in more detail:


package com.sandbox.builder;

import com.sandbox.model.billing.BillingMethod;
import com.sandbox.model.billing.BillingMethodTypes;

public class BillingMethodBuilder {

 private BillingMethod billingMethod;

 public static BillingMethodBuilder create() {
     return new BillingMethodBuilder();
 }

 public BillingMethodBuilder withType(String billingMethodType) {
     BillingMethodTypes thisBillingMethodType = BillingMethodTypes.valueOf(billingMethodType);
     billingMethod.setBillingMethodType(thisBillingMethodType.getType());
     billingMethod.setBillingMethodDescription(thisBillingMethodType.getDescription());
     return this;
 }

 private BillingMethodBuilder() {
     this.billingMethod = new BillingMethod();
 }

 public BillingMethod build() {
     return billingMethod;
 }

}

As you can see, we use a static create method to call a private constructor, and the build method returns the object we are building. In between the create and the build, we have a method name starting with ‘with’. What this means is, when we use this builder, we write a more English-looking line of code: build x, with y. Also, you could probably already see how this builder could be extended, especially for a more complex object.

With this in mind, we can return to our contract creation method, refactor each of these builders to it’s own method, and go one step further: the ContractBuilder:

public Contract finalCreateNewContract(NewContractForm form) {

 Contract contract = ContractBuilder.create()
    .withContractNumber(form.getContractNumber())
    .withCommencementDate(form.getCommencementDate())
    .withStatus(getContractStatus(form))
    .withOwners(getOwners(form))
    .withLivesAssured(getLivesAssured(form))
    .withBillingMethod(getBillingMethod(form))
    .build();

 return contract;
}

Benefits of the Builder

This code reads more naturally than the first attempt using constructors, but it’s worth remembering, this builder also gives a more flexible API. Remember, the constructors of the Contract object only gave us two options: default-constructor-plus-setters, or the pass-all-members option. You could imagine any number of constructors in the Contract object, to cater for different combinations of members, depending on different requirements. Each new constructor makes the Contract API more difficult to interpret for the next guy who needs to use it. By contrast, the builder understands implicitly that you might only need a contract with some of its building blocks, and allows you to create some, none, or all of them.

By extension, if we accept that the builder is the way to access the Contract across our codebase, we can remove all non-default constructors from the Contract object, and remove a lot of complexity from the API.

I’ve been using the Builder pattern a lot in the last few weeks, particularly in unit tests, and so far I’m finding it genuinely makes code more readable.

Note: as mentioned above, the code for this demo is available on GitHub here:
https://github.com/PaddyC/FluentApiSandbox

The next post will look at an application that uses the builder to create Mock objects.

Agile: The SWAT Paradox

SWAT!
If your development team uses agile methods, then you may be familiar with the notion of a SWAT role. It can be summed up quite simply: on every iteration, requests for support are distractions, and so should be forwarded – swatted over – to the guy on SWAT. SWAT is responsible for support, production, other development teams, and also internal to the team wherever required. SWAT is nether junior nor senior, it rotates around from one developer to another each iteration. It is a busy role, but permits project work to maintain focus.

For a development team, having someone permanently on SWAT is incredibly useful, as it outsources all the switching costs associated with the distractions listed above to just one person, who takes it all for the team, if you pardon the expression.

The SWAT Paradox
However, for the developer, time spent in the support role can be very busy, and also feel very wasteful, counter-productive even. For example, in the same morning, SWAT might need to run some SQL to resolve a production issue, diagnose an ugly error in a dusty integration point between two systems in a test environment, explain to another team how to get set up with an automated testing system you guys use, and investigate why a cron job has suddenly started failing, all so the other developers don’t have to… This is difficult work, demanding solid overall development skills, not to mention effective time management.

So where’s the paradox? Well, the paradox is that, while SWAT is undoubtedly essential for Agile development to thrive, it’s also very easy for it, and the SWAT person, to remain invisible in the Agile framework.

Picture the SWAT guy who has worked on the above tasks this morning, and has had a similar afternoon working on priority issues that don’t directly relate to any project under development. When it comes to the next morning’s stand-up meeting, his contribution is moot. Developer one talks about the project, what he did yesterday, what he’s doing today, and describes any blockers. Developer two tells a similar story, as do the business analysts and testers. SWAT on the other hand, gets to say: “I’m on SWAT”… and that’s pretty much it! His contribution is too granular, and anyway, is not directly relevant to the projects under development, so by describing yesterday’s work, even in an abridged form, he would derail the focus of the stand-up meeting (and look a little silly in the process).

So, as each stand-up meeting passes, the SWAT guy might talk about headline production issues, but really, just kind of takes a backseat, unless he has had a chance, amidst all the SWAT work, to work on development stuff. Then, when it comes to the iteration demo, the SWAT guy hasn’t been working on any cards, and so has nothing new for demo. This is a tough paradox to deal with for a developer. It’s not a good feeling to be on the sidelines of a project, particularly when you’re working hard.

More Agile Support
Although it contributes to the success of Agile projects, SWAT work itself is not very agile. It’s reactive and unpredictable, it’s not managed with cards, and it’s not presented to the customer in the end-of-iteration demo. All of this means it’s unfortunately easy for the SWAT guy to fall out of the loop for a SWAT iteration.

One simple way to address this, and to make SWAT a little more agile, is to book a retrospective meeting at the end of each SWAT iteration. Assuming SWAT is a rotating role, this can coincide with each handover.

SWAT work is loaded with opportunities for improvement if you pay attention to it: for example, you’ve run that SQL fix four times in the last three weeks: shouldn’t we just fix the bug? Also, you manually produce that report each month, can we automate it? By looking back at the time spent on SWAT at the end of every SWAT iteration, you can identify opportunities like this and present them to the project manager. This allows for effective prioritisation – maybe that bug can become a card in the next iteration? – and also means that some of those annoying little fires can be put out permanently. This ultimately reduces the SWAT workload.

A SWAT retrospective meeting should include all members of the SWAT rota, the development team lead, and the project manager. These retrospective meetings allow for knowledge sharing and process improvement that should ultimately help reduce SWAT workload, and also they give SWAT work the appropriate spotlight, making it visible to management. We don’t necessarily need daily updates in stand-up meetings, but a little perspective every three weeks will highlight opportunities to reduce support work in an appropriate setting.

To conclude, SWAT plays a critical role in helping agile development teams maintain focus, but paradoxically, does not really fit well into agile practices. Any agile team that employs a support role needs to be aware that this work matters too. By using SWAT retrospective meetings to identify improvements, your team will gradually spend ever less time on SWAT, and your developers will spend ever more time on development.

New Android App: DecideForMe 1.0

Introducing my new android app: DecideForMe. It’s designed to support decision-making when alternatives are being compared.

The app is based around the idea of a Decision as an entity that can be saved, edited, and reported on. A decision (‘which car?’) has choices (‘Ford’, ‘Opel’, ‘Nissan’) , and criteria (‘price’, ‘looks’, ‘top speed’). Criteria are assigned a rating system (eg ‘good/better/best’, ‘green/amber/red’) which allows each choice to build up a score relative to its competitors.

In the app, Choices and Criteria can be rated in a grid screen, which is a simple, visual representation of the decision.

Once rated, the decision can also be converted to a text report format, and this report can also be emailed using any mail client installed on the phone.

My initial goal was simply to get the app to market, so right now, it’s very much version 1.0! Hopefully it has some potential to build on though. Future development could include any or all of the following:

  1. Decision templates: when clicking on the new button, a screen presents some choices which have pre-loaded criteria, for example a ‘laptop comparison’ template could pre-load ‘Price’, ‘Processor’, ‘Ram’, and their relevant rating systems. The user then just inputs the choices.
  2. Pre-populating competitors with URLs linking to stores where the stuff can be bought.
  3. Send and receive decisions: this would be a step towards collaboration, or shared decisions.
  4. Weighted criteria: some criteria are more important than others (and this is often a personal thing, depending on the decision).

Right now, I’m keen to find Android phone owners who could download the app, give it a test drive, and let me know how they get on. You can find the app in the market, just search for ‘DecideForMe’, it’s signed by me, Paddy C, or here are the details

SCJP? You must be Certifiable

I passed the SCJP exam this week. Hooray me!!

From the initial idea to the taking the exam, I’d say the SCJP was on my mind for around 18 months, so the main emotion after passing it is definitely one of relief! On reflection though, I do think it could be done in far less time, so hopefully this post can be a resource for procrastinators like me, who would like to do the SCJP, but in as realistically brief a timeframe as possible.

Why Study the SCJP?
The main reason for me was proof of competence: certification gives a recognised benchmark of development ability, at least in the fundamentals.

A little personal history: I started my programming career in COBOL, and made the switch to Java about four years ago. Soon after switching disciplines, I passed the SCJA exam (don’t sneer!), and found it a genuinely useful way to get my foot in the door to the object-oriented party. However, based on my own experience, and also the reactions of more experienced colleagues to the SCJA and SCJP, I still feel that the SCJP gives a better external validation of Java fundamentals.

In the early weeks and months of studying the SCJP syllabus, I was energised by how much I was learning, and by how much was immediately applicable in my day-to-day coding at work – the standard of my work definitely improved as a result.

Why take the Exam?
However, as I got bogged down in the – let’s be honest here – somewhat dreary SCJP syllabus, I needed more motivation to help me reach the goal of the exam. I understand that it’s worth studying, but why take the exam? Well, the main reason for me is still that passing the SCJP exam unlocks the next level to the Web Component Developer and Web Services Developer exams.

This effectively means you can learn more interesting stuff, and also push for roles that apply this stuff in the real world. My employer would probably not have the budget or the interest to send me on a web services course, so I can choose to brood and wait for a change of heart, or I can go and get certified in the next level stuff under my own steam. (And then send work the bill for the exams 😉 )

SCJP Resources
The SCJP toolkit is:

  • Study guide
  • Sample exams
  • Hands-on coding

I used the official study guide, and would give it a reasonable rating. However, be prepared: the exam questions at the end of each chapter do not just test your knowledge of what you have read: they often introduce new material not covered in the chapter! For this reason, you should be doing all the exam questions while you cover the chapter, and going through each answer set afterwards, even the ones you get right. It’s a laborious process, but it’s the only way to fully cover all the material in the book.

The book alone isn’t enough. Sample exams, and as many as you can find, are also essential. To put the importance of sample exams into context, the book covers about 80% of the material, but sample exams cover the additional 20%. (And yes, the 80/20 rule applies here.) Javaranch has some good introductory ones, and Certpal is also good, but ExamLab is head and shoulders above the rest, definitely the best resource I found. (And it’s free!)

Taking the jump from learning the syllabus to preparing for the exam took me some time, but when I found the ExamLab software, my progress quickened pretty rapidly. If you already know the SCJP syllabus reasonably well, then working through all of the ExamLab sample exams – by that I mean taking the exam, and then reviewing all answers – will make you exam-ready.

This takes time, and requires hands-on coding. So, you might be horrified at the thought of installing Eclipse on your home PC, but get over it. You’ll need to write code – a lot.

Just Do It
Overall, I feel like a more capable developer since doing all the work for the SCJP. More to the point though, I feel ready to do more, so I’ve set myself the new goal of passing the Web Component Developer exam this year. However, I’m following my own advice, and getting the study guide and sample exams up front! Hopefully I’ll procrastinate less this time, but will post on progress and results here.

If you’re thinking of taking the SCJP exam, my advice to you is:

  • Give yourself somewhere between three and six months. You have to be realistic with the timeframe, especially if you’re working and want to continue having a life!
  • Get the book and the ExamLab software straight away.
  • Write code. Work with as many of the sample programs as you can, and don’t be afraid to spend time writing your own programs.

Awesomeness: Balsamiq for Jira

Unequivocal successes are rare enough in software development, so it’s nice to be able to share one. Today I did a show-and-tell presentation to the I.T. department of a Balsamiq plugin for Jira, and it got a pretty positive response.

If you haven’t seen Balsamiq, I’d recommend taking a look and being the one to suggest that your IT department implements it. The video demo says it all, but it’s essentially a screen mockup tool, it can save significant development time (and cost) at the early stages of a project. Ultimately, the mockup can act as a spec in itself, reducing the need for boring, quickly irrelevant documentation.

The combination of Jira integration and the low price were the deal-makers for my bosses. Seriously though, after taking a look, what’s not to like!?

If the talking heads in your IT organisation espouse an interest in agile development methods, and are loath to part with cash at the moment, this should suit you down to the ground. We spent $150 (c. €106) on the three-license option, with the ability to upgrade in future if the demand is there. To quickly compare the cost with the benefit: think of any requirements you might be planning this year that involve screen changes, and how much time you would save doing a mockup before a screen prototype…. sold yet!?

Download the stand-alone demo here, or try online here

Dreaming in Code

dreaming in codeFor anyone who has ever had a passing interest in the dark arts of a software project, I’d heartily recommend Scott Rosenberg’s “Dreaming in Code”. Rosenberg’s entertaining book is a fly-on-the-wall report of the progress (and ultimate crash-and-burn) of a ambitious software project over a three-year period from 2001 – 2004.

The main goal of the open source project, which eventually came to be known as the Chandler project (that’s Raymond Chandler, not Chandler Bing), was to create an intuitive calendar for end users that could be shared over the web on a peer-to-peer basis. The context of this was before the advent of cloud computing or Gmail, so if Chandler was quick to deliver a good product, it could have been a real winner.

The project was led by Mitch Kapor, the creator of Lotus 1-2-3, and a man bringing a track-record of achievement in the software development domain. The team was made up of talented software professionals, all with a passion for the job, and a desire to really deliver something special. Much was expected of Chandler, but after three years, the still unfinished product did not live up to expectations, and Chandler was ultimately deemed a failure.

If you’re a software development professional, there’s much to take from this honest account of a too grand design that never really made it to fruition. However, there’s also much of interest in for those outside of the trade. Rosenberg intersperses his account of the project with witty and interesting asides, which document and explain some idiosyncratic aspects of the software development profession, such as the counter-intuitive nature of open source, the personality of the software developer, and just why it is that ‘software is hard’! (According to Hofstadter’s Law, “it always takes longer than you expect, even if you take into account Hofstadter’s Law”!)

The book is well researched, and Rosenberg’s writing is fresh, lively and involving. The description of the slow death march of the Chandler project is warts-and-all, which makes for rather painful reading at times, but ultimately, the book reflects very well on the participants, in that they allowed this experience to be immortalised.

I really enjoyed ‘Dreaming in Code‘, and would definitely recommend it.