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.
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:
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.
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.
If you want to use these classes you need two things:
- Create a seperate Spring configuration with test-beans (or use your normal Spring config)
- 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:
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:
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.