Integration Testing, As a Matter of Course

It is frustrating how so often projects treat unit testing as an afterthought, scheduled somewhere between development completing and QA testing beginning. Project plans seldom meet their deadlines, so unit testing is often the one to suffer. Or in some cases, testing in general.

Frameworks that can assist in unit testing have been a major time saver for the amount of work that would otherwise need to be put in. I’ve recently re-discovered the joys of development, and in particular test-first development. Using the Spring TestContext Framework it has become even easier to not just do unit testing quickly, but also integration testing.

The importance of both is often understated, as integration testing is more crucial when there are external dependencies. With a few annotations, one can create an integration test simply by specifying the database connection settings and focusing on the critical test paths through the business logic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"test-context-data.xml"})
@Transactional
public final class BidnessLogicManagerTest {
    @Autowired
    private BidnessLogicManager bidnessLogicManager;
 
    @Test
    public void shouldRetrieveIdentity() throws Exception {
        Identity identity =
              this.bidnessLogicManager.findIdentity(10L);
        Assert.assertNotNull(title);
    }
}

The test-context-data.xml contains your usual datasource, transaction manager and other bean configurations.

With this class, you can test logic with rollbacks, test without rollbacks, or you can set up transactions before entering them. If so desired, you can also extend AbstractTransactionalJUnit4SpringContextTests to have access to convenient underlying low-level classes.

This is the way to develop, and its unfortunate that developers will most likely miss out on this, as they are rushing to meet deadlines and pressured to push out code. Using annotations with the Spring TestContext Framework gets us one step closer to having a real internal DSL that allows us to develop in the context of the domain. We can start shedding all the baby fat of traditional Java development, and worry less about all the plumbing.

If Java is to compete with the young, fresh new blood on the horizon, it needs to continue moving in this direction. Java is a powerful language, but it must become more lightweight and faster to develop to stand up against languages like Ruby or even Python.


See Chapter 8. Testing with Spring for more information.