I think that software development is more than just a job. I see myself as an artisan who is trying to get better every day. The “easiest way” to do this is to find a few good tools and find the answers to these questions:
- When should I use the tool X?
- How should I use the tool X?
Automated testing is a very important part of software development, but I haven’t seen a lot of blog posts that let you to take a peek into the toolbox of another developers.
This blog post lets you to take a peek into my toolbox. I will reveal 12 tools that I use for writing unit and integration tests. I will also provide links to other interesting webpages that help you to understand how you can use them.
Let’s get started.
Taking a Peek Into My Toolbox
Before we can use the tools that are described in this blog post, we have to create a build that can run both unit and integration tests. I have written two blog posts that describe how we can do this:
- Integration Testing with Maven describes how we can create a Maven build that has different directories for unit and integration tests.
- Getting Started With Gradle: Integration Testing With the TestSets Plugin describes how we can create a Gradle build that has different directories for unit and integration tests.
We are now ready to take closer look at my toolbox. I have divided my tools into different categories that makes this blog post easier to read.
I give you 12 tools that I use for writing unit and integration tests:
Running Tests
JUnit is a framework that I use for writing both unit and integration tests. I like JUnit because it is the most popular testing framework for Java programming language. In other words, it has a lot extensions and it is easy to find solutions to your problems.
NestedRunner is a JUnit runner that allows us to run test methods placed in nested inner classes. I like NestedRunner because of the following reasons:
- We can replace long method names with BDD style class hierarchy.
- We can remove duplicate code by moving that code to setup methods and putting these methods to the correct inner classes.
- We can link the constants with the test cases that use them by declaring the constants in the correct inner class.
junit-dataprovider is a JUnit runner that allows us to write parametrized tests by using a TestNG like data provider. This is a huge improvement over the default way of writing parametrized tests that pretty much sucks.
Additional Reading:
- Unit Testing with JUnit describes how we can write unit tests with JUnit 4.X.
- Writing Clean Tests: Small Is Beautiful describes how we can solve the problems found from “clean” unit tests.
- junit-dataprovider: Getting Started describes how we can start using junit-dataprovider.
- junit-dataprovider: Features describes the features of the junit-dataprovider.
- Alternative to JUnit Parametrized Classes: junit-dataprovider describes how we can replace the standard parametrized tests with tests that use the junit-dataprovider.
Mocking, Stubbing, and Faking Stuff
Mockito is the most popular mocking framework for writing unit tests. I like it because it has a simple API, a lot of useful features, and excellent documentation.
Greenmail is an in-memory email server that supports SMTP, POP3, and IMAP with SSL socket support. I like it because it is really easy to use. When I was looking for a “fake” email server, I tried several alternatives and Greenmail was the only one that worked as I wanted.
MockFtpServer is a library that provides two different FTP server implementations (fake/stub) that are useful for testing different scenarios. If we need test code that interacts with a FTP server, MockFtpServer is our weapon of choice.
Additional Reading:
- Mockito Reference Manual is an excellent example of a reference documentation that actually helps you to get the job done.
- Integration Testing IMAP, SMTP and POP3 with Greenmail describes how you can write integration tests for code that sends email messages.
- FakeFtpServer - Getting Started describes how you can use the FakeFtpServer.
- StubFtpServer - Getting Started describes how you can use the StubFtpServer.
- FakeFtpServer or StubFtpServer? helps you to decide which FTP server implementation you should use in your tests.
Writing Assertions
Hamcrest provides matchers that we can use to write assertions for our unit and integration tests. I use it when I need to write assertions for unit or integration tests that use the Spring MVC Test framework.
AssertJ provides a fluent API for writing assertions that have helpful error messages. It improves the readability of our test code and helps us to transform our test cases into executable specifications that speak the correct domain-specific language.
Additional Reading:
- The Hamcrest Tutorial describes how you can get started with Hamcrest.
- Using Hamcrest for testing describes how you can use regular Hamcrest matchers in your unit tests and extend its capabilities by creating custom matchers.
- AssertJ Core Overview provides an overview to AssertJ Core.
- Turning Assertions Into a Domain-Specific Language describes how we can create custom AssertJ assertions.
- Writing Clean Tests: Replace Assertions With a Domain-Specific Language describes why we should replace regular JUnit assertions with custom assertions that use the correct domain-specific language.
Testing Data Access Code
H2 is a very fast in-memory database that is useful for writing integration tests that are run in the local development environment.
DbUnit is a JUnit extension that can be used to initialize the database into a known state before each integration test and ensure that the database contains the correct data. DbUnit has its warts, but it is a very useful tool because it helps us to separate the test data creation from the tested code.
Additional Reading:
- DbUnit Core Components describes the core components of DbUnit. If we want to write tests that use DbUnit, we should know what these components are.
- DbUnit Best Practices provides five rules that help us to write better tests for our data access code.
- Writing Tests for Data Access Code helps us to write deterministic data access tests which test the right thing, are easy to read, and easy to maintain.
Testing Spring Applications
Spring Test is the Swiss army knife of writing automated tests to Spring applications. It provides a first class support for writing unit and integration tests to Spring powered applications.
Spring Test DbUnit integrates DbUnit with the Spring Test Framework. If we need to write data access tests for a Spring application that uses relational database, Spring Test DbUnit helps us to do it.
Additional Reading:
- The Spring Framework Reference Manual: 11. Testing describes every feature of the Spring Test framework.
- Spring MVC Test Tutorial describes how we can write unit and integration tests for Spring MVC controllers.
- Spring Data JPA Tutorial: Integration Testing describes we you can write data access tests for our Spring powered repositories.
- Spring From the Trenches: Using Null Values in DbUnit Datasets describes how we can use null values in our DbUnit datasets when we are writing data access tests for a Spring application.
- Spring From the Trenches: Resetting Auto Increment Columns Before Each Test Method describes how we can reset the auto increment columns before each test method when we are writing data access tests for a Spring application.
What Testing Tools Do You Use?
I have now revealed the tools that I use for writing unit and integration tests. I challenge you to do the same. You can participate in this challenge by following these simple steps:
- If you have a blog, you can write a blog post and leave a comment to this blog post.
- If you don’t have a blog, you can leave a comment to this blog post.
Why should you do this?
Well, if helping other people isn’t enough for you, I promise to help you to get more readers by linking to your blog post, tweeting it, and sharing it with the Java Testing Society.