Testing with Spring

To improve the quality of your code it is important to thoroughly test your code. Not only using unit tests but also using integration tests. I’ll describe these terms and show examples on how to create tests using JUnit and Spring’s Test library.

Unit Testing

A unit-test is an automated test that tests one single unit-of-work. So, what is this unit-of-work? Its the smallest piece of code that can be distinguished. If you are programming in Java the smallest unit you can test is a single method. So all unit-tests should only test this single unit, and no other dependencies.

Here is an example of a unit-test:

publicclassCalculatorTest{privatefinalIntegerTOADD1=6;privatefinalIntegerTOADD2=4;privatefinalIntegerANSWER=10;@TestpublicvoidtestAdd(){Calculatorcalculator=newCalculator();IntegerreturnedAnswer=calculator.add(TOADD1,TOADD2);Assert.assertEquals("The answer of "+TOADD1+"+"+TOADD2+" should be "+ANSWER+" but was "+returnedAnswer,ANSWER,returnedAnswer);}}

By adding the @Test annotation JUnit will know this is a unit test. When running the code we verify that it correctly adds two values.

Test Driven Development

Test Driven Development (TDD) is very important in modern software development. More and more people and businesses are using this technique to improve their code quality.

What TDD consists of, basically, is a very small development cycle for all your code. You just follow these steps:

Write a unit-test that verifies your usecase

Implement the interfaces you created for step 1
(can be done quick and dirty, just to get the logic correct)

Run the tests and confirm your code is logically correct

Refactor your code into maintainable, readable, reusable code

Confirm the code still works as designed by running the test(s)

Actually, I don’t write all my tests before the real code, but the process goes hand-in-hand. When writing code I always think about how I’m going to test it, and when the code is done, the test is too, combining step one and two. It speeds the process up a bit, and the code I end up with is almost the same because of the refactor-step.

Integration testing

With unit testing you can verify that the smallest units of work are correct. But this doesn’t give you any guarantees!

A famous example is NASA’s $125 million Mars Orbiter. It crashed when it was about to land on Mars. And why did it crash? Because one component calculated metric units and it feeded this information into another component that was using English units.

Very painfull for NASA, but a great example showing the importance of integration testing. Both NASA-components had been tested a lot, but apparently they neglected to test the components together.

Integration testing with Spring

Most business software in Java uses Spring. This is a framework glueing all your components together and providing dependency injection. This makes it easy to test all the classes and methods, but it makes it a bit harder to test the integration and communication between classes because you depend on the framework for that.

Luckely Spring helps us with a their Spring Test library full of tools that help you creating integration tests.

One thing you need in all integration tests is Springs Context. With Spring Test it is possible to wire some beans and let Spring inject those beans in your unit test. The old(-ish) way to do this is extending org.springframework.test.AbstractDependencyInjectionSpringContextTests or org.springframework.test.AbstractTransactionalSpringContextTests.

Override String[] getConfigLocations(), provide it with the location of your Spring configuration.

Now, for all the private fields you have in your testclass Spring will attempt to auto-wire beans on it.

A small example:

publicclassPersonServiceTestextendsAbstractDependencyInjectionSpringContextTests{privatePersonServicepersonService;/**
* Integration test for my calculator.
* It also calls the calculatorDao which is in the calculatorService.
*/@TestpublicvoidtestAddPerson(){Personp=...personService.add(p);//Test if the person has been added.
}/**
* Provide the test with a configuration
*/@OverrideprotectedString[]getConfigLocations(){returnnewString[]{"my-test-applicationcontext.xml"};}}

Testing with Spring 2.5+

With the introduction of Spring 2.5 they improved the above method. For more flexability and a bit more control over the objects they introduced the SpringJUnit4ClassRunner.

This is how you would use it:

importorg.junit.runner.RunWith;importorg.springframework.test.AbstractDependencyInjectionSpringContextTests;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"my-test-applicationcontext.xml"})publicclassPersonServiceTest{@AutowiredprivatePersonServicepersonService;/**
* Integration test for my calculator.
* It also calls the calculatorDao which is in the calculatorService.
*/@TestpublicvoidtestAddPerson(){Personp=...personService.add(p);//Test if the person has been added.
}}

It works the same way as before, but instead of having to override the AbstractDependencyInjectionSpringContextTests you now have a POJO. All the information about Spring and JUnit are placed in annotations on top of you class.

Hopefully I’ll find the time to write another post on mocking, including EasyMock and Mockito.