Java test with JUnit

By , last updated December 6, 2019

Software testing is a powerful tool when creating all types of applications. That’s why it is also so important to test it in details so that a chance of an unexpected fail is minimal. We say it so: all code you write must be tested with minimum 2 types of tests: positive and negative. A positive test is a test that verifies a correct functionality, a negative test verifies that a function will fail or throw an exception. Let’s see how it works.

Suppose we have a class Apple with Color as a parameter (red and green apples er allowed).

public class Apple {
    private String color;

    boolean isGreenOrRedColor() {
        if(color.equalsIgnoreCase("green") || color.equalsIgnoreCase("red"))
            return true;
        return false;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

In this case we need to test both a class and a method isGreenOrRedColor(). We create a class to test our Apple class and give it a name “AppleTest.java”. In order to begin using JUnit-tool we import org.junit.Test. In this example we create two tests for our class: green (positive) and blue (negative) apples.

A positive test verifies a correct answer:

    @Test
    public void testThatGreenIsCorrectColor() {
        Apple apple = new Apple(); //test instantiation of the class
        apple.setColor("grEeN");
        assertTrue(apple.isGreenOrRedColor()); //we expect that it returns TRUE
    }

Annotation @Test gives JUnit a sign that there is a job to do.

Next we write a negative test where we send a fail color to the class and expect that it returns FALSE:

    @Test
    public void testThatBlueIsNotSupportedColor() {
        Apple apple = new Apple(); //test instantiation of the class
        apple.setColor("blue");
        assertFalse(apple.isGreenOrRedColor()); //we expect that it returns FALSE
    }

What is also very important is that often we need access to the testing method in order to test it from another class. It is a bad practice to give such methods public access only in order to test it. If a method can be private you can give it default/package access or test it from another one with a public access.

To run the tests in Eclipse you choose: Run as -> JUnit test

Complete test class for this example.

import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

class AppleTest {

    @Test
    public void testThatGreenIsCorrectColor() {
        Apple apple = new Apple(); //test instantiation of the class
        apple.setColor("grEeN");
        assertTrue(apple.isGreenOrRedColor()); //we expect that it returns TRUE
    }

    @Test
    public void testThatBlueIsNotSupportedColor() {
        Apple apple = new Apple(); //test instantiation of the class
        apple.setColor("blue");
        assertFalse(apple.isGreenOrRedColor()); //we expect that it returns FALSE
    }
}

Senior Software Engineer developing all kinds of stuff.