02 - JUnit Framework

2. JUnit Framework

JUnit (junit.org) in version 4.x is a test framework and open source software for the Java programming language. it uses annotations to identify methods to specify a test.

JUnit was designed by Kent Beck and Erich Gamma for writing and running test cases for Java programs and it includes core test framework class hierarchy. JUnit defines a common language for writing tests. For web applications JUnit offers the advantage to test the application without server.

The framework creates the relationship between two processes: development and testing and automates the process of running tests.

It is one of a family of unit testing frameworks all known as xUnit and represents the Java version of the xUnit architecture for unit- and regression-testing frameworks.

JUnit is known as a standard for unit testing in Java and is included in many testing products which extend or are based on JUnit. The framework is not included in JDK, but many IDEs have built-in support for JUnit.

2.1 Overview

JUnit is simple to use and increases confidence in the correctness of the code. It can improve the design of the tested class because it makes more clear about how the objects are actually used.

Classes or methods are tested in isolation, although they do not function in isolation. They collaborate with other classes or methods which sometimes are not available during the unit tests. For example the request and response objects are created only by the servlet container and the servlet container isn’t running during the unit tests.

Classical testing (print statements for test output) and debugging are eliminated and first class objects are created. Tests become reusable and extendable.

2.2 Define a test in JUnit

A JUnit test is represented by a method contained in a class used only with the scope of testing.

Tested written with JUnit 4.x version should be annotated with @org.junit.Test annotation.

The annotated method uses the method from JUnit framework to check the expected result of the code versus the actual result.

In order to launch the test in Eclipse right – click on the test class MyClassTest.java and select Run As > JunitTest.

The result is obtained in the JUnit console:

The result is green if no error is present and red if the expected result is different than the obtained result.

It is recommended that the test class should have the “Test” suffix in their name because the Maven build system will automatically include such classes in its test scope.

The name of the test class should also explain what the test does in order to know the test scope without reading the implementation.

2.3 Test Steps

General test steps

A test can be run using a set of background resources or data commonly named as fixture. A fixture is created and destroyed by a TestCase through its methods setup() and teardown() which are executed during the running of the TestCase

The TestCase class calls setup() method before running each test for initialization and tearDown() when the test is complete for cleanup.

The test methods are put into the TestCase in order to share the fixture code. This is one of the design goals of JUnit: to reuse the fixture between the tests and to write more quickly and more tests.

The general test steps in Junit testing are:

1.Setup

2. Exercise

3. Verify

4. Teardown

TestCase lifecycle can be represented by the following diagram:

In JUnit 4.x the methods setup() and tearDown() have been replaced by annotations:

setup()-@Before

tearDown() - @After

testXYZ() - @Test

The extending of the TestCase class is no longer necessary beginning with this version.

In JUnit 3.x the example of testing used before (see 2.2) differs in not using annotations and extending the TestCase class: