Introduction to unit testing

by Martin Monperrus

Writing test cases are like levels in a video-game. There are easy test cases, and very hard ones. Only expert gamers kill the strongest monsters of the last level. Only expert programmers write the hardest tests using advanced techniques such as dependency injection and mocks.

In this document, the examples are given in Java and uses the JUnit test framework.

Level 0 Test - Happy Path Test

The level 0 tests specify the nominal behavior under standard conditions (also called the "happy path").

A level 0 test contains three components:

the contract that is being tested

the setup of the test, initializing and executing the program under test

at least one assertion which compares the actual behavior against the expected behavior.

Notes

What about setup ((???)) and tearDown ((???)) code? Personally, I don't like them, because they hinder the readability and maintainability of test code, because one has to scroll up and down to understand the test.