Menu +

Search Posts

JUnit Introduction

1. The JUnit Framework

JUnit is a test framework which uses annotations to identify methods that specify a test. JUnit is an open source project hosted at Github.

2. How to define a test in JUnit?

A JUnit test is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation.

This method executes the code under test. You use an assert method, provided by JUnit or another assert framework, to check an expected result versus the actual result. These method calls are typically called asserts or assert statements.

You should provide meaningful messages in assert statements. That makes it easier for the user to identify and fix the problem. This is especially true if someone looks at the problem, who did not write the code under test or the test code.

3. Example JUnit test

The following code shows a JUnit test using the JUnit 5 version. This test assumes that the MyClass class exists and has a multiply(int, int) method.

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MyTests {

    @Test
    public void multiplicationOfZeroIntegersShouldReturnZero() {
        MyClass tester = new MyClass(); // MyClass is tested

        // assert statements
        assertEquals(0, tester.multiply(10, 0), "10 x 0 must be 0");
        assertEquals(0, tester.multiply(0, 10), "0 x 10 must be 0");
        assertEquals(0, tester.multiply(0, 0), "0 x 0 must be 0");
    }
}

4. Annotations (JUnit 4)

JUnit 4Description
import org.junit.*Import statement for using the following annotations.
@TestIdentifies a method as a test method.
@BeforeExecuted before each test. It is used to prepare the test environment (e.g., read input data, initialize the class).
@AfterExecuted after each test. It is used to cleanup the test environment (e.g., delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures.
@BeforeClassExecuted once, before the start of all tests. It is used to perform time intensive activities, for example, to connect to a database. Methods marked with this annotation need to be defined as static to work with JUnit.
@AfterClassExecuted once, after all tests have been finished. It is used to perform clean-up activities, for example, to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit.
@Ignore or @Ignore("Why disabled")Marks that the test should be disabled. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included. It is best practice to provide the optional description, why the test is disabled.
@Test (expected = Exception.class)Fails if the method does not throw the named exception.
@Test(timeout=100)Fails if the method takes longer than 100 milliseconds.

5. Assert statements (JUnit 4)

StatementDescription
fail([message])Let the method fail. Might be used to check that a certain part of the code is not reached or to have a failing test before the test code is implemented. The message parameter is optional.
assertTrue([message,] boolean condition)Checks that the boolean condition is true.
assertFalse([message,] boolean condition)Checks that the boolean condition is false.
assertEquals([message,] expected, actual)Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays.
assertEquals([message,] expected, actual, tolerance)Test that float or double values match. The tolerance is the number of decimals which must be the same.
assertNull([message,] object)Checks that the object is null.
assertNotNull([message,] object)Checks that the object is not null.
assertSame([message,] expected, actual)Checks that both variables refer to the same object.
assertNotSame([message,] expected, actual)Checks that both variables refer to different objects.

https://www.vogella.com/tutorials/JUnit/article.html

Leave a Reply

Your email address will not be published. Required fields are marked *