JUnit framework also allows quick and easy generation of test cases and test data. The org.Junit package consist of many interfaces and classes for JUnit Testing such as Test, Assert, After, Before, etc.

What is Test fixture

Before we understand what a test fixture is, let’s study the code below This code is designed to execute two test cases on a simple file. Few issues here

The code is not readable The code is not easy to maintain. When the test suite is complex the code could contain logical issues.

Compare the same code using JUnit The code far more readable and maintainable. The above code structure is a Test fixture. A test fixture is a context where a JUnit Test Case runs. Typically, test fixtures include:

Objects or resources that are available for any test case. Activities required that makes these objects/resources available. These activities are

allocation (setup) de-allocation (teardown).

Setup and Teardown

Usually, there are some repeated tasks that must be done prior to each test case. Example: create a database connection. Likewise, at the end of each test case, there may be some repeated tasks. Example: to clean up once test execution is over. JUnit provides annotations that help in setup and teardown. It ensures that resources are released, and the test system is in a ready state for next test case.

These JUnit annotations are discussed below-

Setup

@Before annotation in JUnit is used on a method containing Java code to run before each test case. i.e it runs before each test execution.

Teardown (regardless of the verdict)

@After annotation is used on a method containing java code to run after each test case. These methods will run even if any exceptions are thrown in the test case or in the case of assertion failures. Note:

It is allowed to have any number of annotations listed above. All the methods annotated with @Before in JUnit will run before each test case, but they may run in any order. You can inherit @Before and @After methods from a super class, Execution is as follows: It is a standard execution process in JUnit.

Execute the JUnit @Before methods in the superclass Execute the @Before methods in this class Execute a @Test method in this class Execute the @After methods in this class Execute the @After methods in the superclass

Example: Creating a class with file as a test fixture In the above example the chain of execution will be as follows-

createOutputFile() testFile1() deleteOutputFile() createOutputFile() testFile2() deleteOutputFile()

Assumption: testFile1() runs before testFile2()– which is not guaranteed.

Once-only setup

It is possible to run a method only once for the entire test class before any of the tests are executed, and prior to any @Before method(s). “Once only setup” are useful for starting servers, opening communications, etc. It’s time-consuming to close and re-open resources for each test. This can be done using the annotation @BeforeClass in JUnit.

Once-only tear down

Similar to once only setup , a once-only cleanup method is also available. It runs after all test case methods and @After annotations have been executed. It is useful for stopping servers, closing communication links, etc. This can be done using the @AfterClass annotation

JUnit Test Suites

If we want to execute multiple tests in a specified order, it can be done by combining all the tests in one place. This place is called as the test suites. More details on how to execute test suites and how it is used in JUnit will be covered in this tutorial.

Junit Test Runner

JUnit provides a tool for execution of your test cases.

JUnitCore class is used to execute these tests. A method called runClasses provided by org.junit.runner.JUnitCore, is used to run one or several test classes. Return type of this method is the Result object (org.junit.runner.Result), which is used to access information about the tests. See following code example for more clarity.

In above code “result” object is processed to get failures and successful outcomes of test cases we are executing.

First JUnit program

Fair knowledge of SDLC, java programming, and basics of software testing process helps in understanding JUnit program. Let’s understand Unit Testing using a live example. We need to create a test class with a test method annotated with @Test as given below: MyFirstClassTest.java TestRunner.java To execute our test method (above) ,we need to create a test runner. In the test runner we have to add test class as a parameter in JUnitCore’s runclasses() method . It will return the test result, based on whether the test is passed or failed. For more details on this see the code below : Output Once TestRunner.java executes our test methods we get output as failed or passed. Please find below output explanation:

In this example, after executing MyFirstClassTest.java , test is passed and result is in green. If it would have failed it should have shown the result as Red and failure can be observed in failure trace. See below JUnit gui :

Summary

JUnit is a framework which supports several annotations to identify a method which contains a test. JUnit provides an annotation called @Test, which tells the JUnit that the public void method in which it is used can run as a test case. A test fixture is a context where a test case runs To execute multiple tests in a specified order, it can be done by combining all the tests in one place. This place is called as the test suites. JUnit provides a tool for execution of the tests where we can run our test cases referred as Test Runner.