application

 

JUnit is an Open Source framework designed for writing and running automation tests. This framework is a part of xUnit family of frameworks. At first, it was designed for Java, but now it can be used with other programming languages as well. The most important features of this framework are:

  •  JUnit is Open Source and free
  • It is used for writing and running  tests
  • It provides annotations which help you to identify the test methods
  • It provides assertions for testing expected results
  • It provides test runners for running tests
  • The test can run automatically and we just check the results
  • Tests can be organised into test suites
  • It provides a simple and clean way to report the test run
  • It is able to run multiple tests concurrently.

Why use JUnit

The main reason for using JUnit is that it provides a convenient way to create the tests, the framework is built in Eclipse and in some other IDE’s, it is versatile and it can be used for different types of testing. JUnit helps measuring the progress and simplifies the maintenance process of the existing tests. 

How to use it

In the test automation projects, JUnit can be used with Maven. Maven is managing the integration of different dependencies in a project. Instead of using jar files, maven dependencies for JUnit are added in the pom.xml file. For example:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

In this way we have access to all JUnit features:

import org.junit.Test;
import static org.junit.Assert.*;

public class MyUnitTest {

    @Test
    public void testConcatenate() {
        MyUnit myUnit = new MyUnit();

        String result = myUnit.concatenate(“one”, “two”);

        assertEquals(“onetwo”, result);
    }
}

In this example there are used two JUnit features. The @Test annotation, done to signal to the unit test runner that this is method, represents a unit test, that should be executed. Methods that are not annotated with @Test are not executed by the test runner.

When the assertEquals() method is called, the actual testing is done, comparing the output of the called method concatenate() with the expected output. As said before, JUnit gives the possibility to use different annotations which are very useful when writing tests.

Here is a list of most commonly used annotations:

@RunWith – a test case can specify its expected runner type with the @RunWith annotation – eg: @RunWith(SerenityRunner.class) – in this case we expect the test to run with the Serenity runner.

@Before – indicates that the attached method will run before any test in the class or subclass.

@After – indicates that the attached method will run after any test in the class or subclass.

@BeforeClass – has the same functionality as @Before; the difference is that @BeforeClass will be called once per test class based, and @Before will be called once per test based.

@AfterClass – has the same functionality as @After; the difference is the same as in the previous case.

@Ignore – marks a test that has to be ignored by the runner.

Another important feature that JUnit provides is the Junit Test Suite, marked by the @Suite – annotation
Eg:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
   TestJunit1.class,
   TestJunit2.class
})
public class JunitTestSuite {   
}      

JUnit provides different assertions for comparing the input and the output of a test: assertEquals, assertTrue, assertFalse, assertNull, assertArrayEquals – these are just a few example of assert methods that actually do the real testing.

JUnit also has a simple way of reporting. In Eclipse, when we running a JUnit test, a JUnit tab will open and tests results are displayed. The name of the test, the status, green for passed, red for error and blue for failed assertion, are showed. 

Author: Horatiu Encian

image source