We’ve put together a running list of tools and frameworks that make it easier to test Java code.
JUnit is a great starting point for anyone who’s new to unit testing, which is probably why it’s one of the most popular unit testing frameworks for Java. If you’ve never written a unit test in JUnit before, you can learn how in the first chapter of the Definitive Guide to Unit Testing.
Maven is a framework for plugins that carry out a variety of actions for Java projects. Maven is great for centralizing the management of building, reporting and documenting a project in Project Object Model (“pom”) files.
Spring is the most popular Java framework in the world, so you’re probably already using it. But if you haven’t started to formally test your code yet, then consider using Spring Boot, which allows you to create specific slices of your app to test.
Mockito is a go-to tool for mocking in unit tests. (If you’re unfamiliar with mocking, we’ve got a blog on the basics and another on best practices). Mockito lets you create test doubles and is particularly useful if you intend to practice test-driven development.
PowerMock is another mocking tool that’s more powerful than Mockito. It can be mock hard cases, but should only be used when necessary, because accidental misuse by a non-expert can introduce hard-to-fix issues.
If you’ve started writing and running tests, it’s time to measure your unit test code coverage, which JaCoCo—a free code coverage library—does well. It’s only available for Eclipse, but can plug into various frameworks.