Unit testing

Unit testing is an indispensable aspect of software development. Testing gives us confidence that our code works fine and provides us a safety net when refactoring. Fortunately, Gradle's Java plugin makes it simple and easy to unit test your code.

We will write a simple test for the same example app we created above. We will create our first unit test now using JUnit (v4.12) library.

Note

More information about JUnit can be found at http://junit.org.

Adding a unit test source

Again, like Maven, Java test sources are kept in the src/test/java directory relative to the project root. We will create this directory and, as a good practice, the test package structure will reflect the same hierarchy as the source packages.

...
src
└── test

Get Gradle Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.