An Introduction to JUnit

Testing is an essential part of creating software and one that is often looked at as a second class citizen in the realm of application code. Despite this mischaracterization, test code often plays just as much — or an even larger — role in successfully releasing software that contains as few bugs as possible.

In this tutorial, we will walk through each of the steps required to develop simple but well-thought-out tests, starting with the theory and concepts behind creating tests. With this theory in hand, we will create a test fixture in JUnit 4 to exercise an example application and add test cases to this fixture as necessary. We will also learn how to mock external dependencies and use the setup mechanism provided by JUnit to abstract the initialization logic for our test cases. Along the way, we will cover the given-when-then approach to testing and the standard naming conventions used in many large-scale projects.