Saturday 11 September 2021

How to test Spring's Transactional behaviour


In order to properly test the behaviour of the software that we develop using frameworks such as Spring,  we need to run our code with the framework code and configuration in place.

A recent situation that I wanted to verify as working correctly involved a data access component which relied on Spring to rollback the underlying database transaction if an exception had occurred.

Level 1 - Unit testing

We could unit test the class by creating an instance of it and passing in mock collaborators to trigger exceptions, but that would silently ignore the @Transactional annotation, since the annotation is effectively just metadata for the relevant Spring logic to detect and wrap logic around.


void throwException() {

   Foo foo = new Foo("xyz", "Unit test example foo.");

   FooDAO fooDAO = new FooDAO(someMockConnection);

   try {


     fail("Excepted exception to have been thrown");

  } catch (MyAppDataAccessException exception) {

    assertThat(exception.getMessage()).isEqualTo("Foo with id 'xyz' does not exist to be updated.");



Level 2 - Component testing

Slice testing

- Have the test include part of the framework to create the component so that the framework will introduce the appropriate logic (extending or wrapping the object with a proxy for transaction handling).

Level 3 - Testing component within the app

To ensure that the layers all fit together in the way that we expect them to we can run the full application, using locally provisioned resources - e.g. with TestContainers providing a real database server.

There are some situations that can only be reached if another user or system happens to be interacting with the same data that we are operating on.  In my case the data was read in as part of validation prior to attempting to apply the database updates, so if the database representation didn't match at the start of processing then validation would fail and no update call would be made.

As an initial approach I have introduced a component into the test codebase which will be included as part of the running application.  It implements the validation interface with a behaviour combination of delaying processing before calling through to a delegate - in this case the real validation component.

Friday 3 September 2021

The move back to New Zealand

The new flat

By August 2021 I was in a different flat and working from home on a fulltime basis.  There were some good and bad aspects of the new place:


 - Very limited shared living areas, so minimal risk of catching Covid if one of the new housemates got it

 - My bedroom overlooked a quiet(ish) garden area, and the flat was on a quieter side street


 - Maintenance works that I was home for, including installation of a new boiler and clearing of existing radiators

 - Being home for deliveries - including a fridge freezer that the delivery guys weren't keen to lug up the narrow staircase

 - Long running complaints about noise from downstairs neighbours carried the potential for having to be significantly disrupted by installation of under floor soundproofing

Another lockdown

After a couple of months of being able to go out and meet with a very limited number of people "support bubble" to keep me sane, another UK lockdown followed including the cancellation of my very limited plans for Christmas.

Several months later the UK lockdown restrictions eased again.  Around this time I started to evaluate timelines and notice periods and the scarcity of places in New Zealand's Managed Isolation and Quarantine (MIQ) system.

Timing and luck

Because I needed to give 12 weeks notice for resigning from my job I would only look at the furthest future available dates in MIQ, as those timelines typically only showed the next three months or so of availability.

Sometime in May my employer's bonus announcement came through, so I set myself a timeline of resigning shortly after the bonus payment had made it into my bank account.  Around this time I was also fortunate to find that MIQ spaces for August had opened up, so I managed to reserve my MIQ stay from the last day of August and promptly booked a one way flight from London to Christchurch.

After serving out the notice period at my job I ended up with four weeks or so to finish organising my move - packing boxes and offloading things.

Avoiding Covid

I had hoped to enjoy some long walks around London on my own for the last couple of weeks - keeping isolated to reduce the risk of catching Covid, but then my knee decided that it had other ideas that involved leaving me unable to walk due to a minor injury that I had picked up almost exactly a year earlier.  I didn't want to risk picking up Covid from a visit to a doctor of MRI scanning place, so I spent a lot of time lying down and bought myself some crutches for the limited excursions out of the flat.

My first ever Covid test was the one that I had too book in for shortly prior to my flight home.  I was bricking it!  If that result came through as carryign the illness then I would have a few problems:

 - Nowhere to live, and no clarity around where in London would accept someone who is known to be carrying Covid-19

 - No source of income

 - No timeline for when I might next be able to attempt to travel to New Zealand (MIQ spaces were then an extremely sought after and scarce resource)

The flight home

The PCR test came back as negative for Covid-19, so the final requirement for being allowed onto the flight was in place.

On the first leg of the two hop journey I had a row of seats to myself, on the second flight I had almost an entire section of the aircraft to myself.  I'm curious whether the government may be subsidising the airlines to maintain the service, or if they are carrying more unaccompanied baggage than usual to make up for the lack of passengers.

Getting to MIQ

I used to live in Christchurch, so I had some ideas around the locations of hotels.  When the bus driver said it would be about a 20 minute ride to the MIQ facility I was relieved to realise that I wouldn't be stuck out in one of the hotels close to the airport.  I checked over the list of other possibilities and recalled that my old friends in the hospitality industry used to say nice things about Chateau on The Park...   I can confirm that it is a nice spot - I'm feeling comfortable, as I write this with my balcony doors open overlooking the exercise area in the Spring sunshine.

On the first night I couldn't sleep properly, so I went online to check the availability of online grocery orders.  The following afternoon I had a bunch of paper bags outside my door, filled with the goodies that I had ordered.