Writing a Basic JUnit Test Example

As testing is critical for good quality software we will show you the basics of writing a JUnit Test. The requirements to define a test class are that the class must be public and contain a no argument constructor. The requirements to create a test method are that it must be annotated with @Test, be public, take no arguments and return void.

Maven Dependency

First step is to include the necessary dependencies, note that we include the junit dependency in test scope. Test scope will not be included in the production jar or war file generated by maven.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.test.junit</groupId>
    <artifactId>basic-example</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Writing a Basic JUnit Test

Here we list the basic JUnit Test assert statements if you want to dive into more detail scroll down.

package com.memorynotfound.test;

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

public class BasicTest {

    @Test
    public void basicUnitTest(){
        assertNull("This should evaluate to null", null);
        assertNotNull("Newly created object should not be null", new Object());
        assertSame("This should be the same object", sameObject, sameObject);
        assertEquals("1 should be equal to 1", 1, 1);
        assertNotEquals("Objects should not be equal", new Object(), new Object());
        assertTrue("This should evaluate to true", true);
        assertFalse("This should evaluate to false", false);
        assertArrayEquals("Arrays should be equal", new int[]{1, 2}, new int[]{1, 2});
    }
}

Import vs. static import

A great best practice to writing clean and readable JUnit tests is to import static your assert statements. This results in easier to read test code. This is great because you will mostly write your code once and read it many times.

import org.junit.Assert;
Assert.assertNull("This should evaluate to null", null);

import static org.junit.Assert.assertNull;
assertNull("This should evaluate to null", null);

Difference Between assertEquals and assertSame

Whereas assertEquals() calls the object equals() method to verify for object equality, assertSame() uses the equality operator == to verify if the object points to the same object.

assertNull

This assert must evaluate to null or else the test will fail.

@Test
public void assertNullTest(){
    assertNull("This should evaluate to null", null);
}

assertNotNull

This assert must evaluate to not null or else the test will fail.

@Test
public void assertNotNullTest(){
    assertNotNull("Newly created object should not be null", new Object());
}

assertSame

The equality operator == is used to test for equality. So the equality operator must evaluate to true or else the test will fail.

@Test
public void assertSameTest(){
    Object sameObject = new Object();
    assertSame("This should be the same object", sameObject, sameObject);
}

assertEquals

The equals() method is used to test for equality. So the equals() method must evaluate to true or else the test will fail.

@Test
public void assertEqualTest(){
    assertEquals("1 should be equal to 1", 1, 1);
}

assertNotEquals

The equals() method is used to test for equality. So the equals() method must evaluate to false or else the test will fail.

@Test
public void assertNotEqualTest(){
    assertNotEquals("Objects should not be equal", new Object(), new Object());
}

assertTrue

This evaluates a boolean condition. The boolean condition must evaluate to true or else the test will fail.

@Test
public void assertTrueTest(){
    assertTrue("This should evaluate to true", true);
}

assertFalse

This evaluates a boolean condition. The boolean condition must evaluate to false or else the test will fail.

@Test
public void assertFalseTest(){
    assertFalse("This should evaluate to false", false);
}

assertArrayEquals

This assert statement checks whether two arrays are equal.

@Test
public void assertArrayEqualTest(){
    assertArrayEquals("Arrays should be equal", new int[]{1, 2}, new int[]{1, 2});
}

Testing whether an exception occurred

You can also test whether an exception is expected, there ar two ways to achieve this.

By Annotation

@Test(expected = ArithmeticException.class)
public void exceptionTest(){
    float temp = 5 / 0;
}

By fail() method

@Test
public void exceptionFailTest(){
    try {
        float temp = 5 / 0;
        fail("Exception should have been thrown");
    } catch (ArithmeticException e){
        // should fail
    }
}

References

Download

Download it ! – JUnit test example

You may also like...