Simple JUnit test with boundary testing strategy

Objective

In this tutorial, we will learn how to use JUnit test and apply it into a simple program with the boundary testing strategy.

JUnit test library

  • Please download the latest version of JUnit test from https://junit.org/junit5/
  • And then, import this .jar file into your project in Eclipse/Netbean….

We need two main classes in this library:

  • Test: we have to put the prefix @Test just before each JUnit test method.
  • Assert: we could use some of the following static method of this class:
    • assertTrue(x): if we expect x is true;
    • assertFalse(x): if we expect x is false;
    • assertNull(o) if we expect the object o is null;
    • assertNotNull(o): if we expect the object o is not null;
    • assertEquals(x,y): if we expect x is equal to y. x and y could be two object.

System architecture

In general, if we have a class to test with JUnit, we will need a corresponding test class for it. For instance, if we have a simple class of Util with a very simple method of sum(int a, int b). We will need a TestUtil for it with some test case of the method sum.

Boundary testing strategy

An important question here is how many test case we need to test for the method sum? In theory, the type of int (4bytes) in Java can varies from -2^32 to 2^31 . It means that we may have to test for every value of a from – 2^32 to 2^31 and every value of b from – 2^32 to 2^31 . Consequently, we have to test about 2^62 test case possible. It is obviously impossible.
However, by applying the boundary testing strategy, we could reduce very much the number of testcase to test while keeping the error rate below the acceptable threshold. In this strategy, if a variable has a boundary value, assumed as x, we need to test this variable with only 4 values possible: x, x-1, x+1, and a random value which is far from x but in the domain of the variable.
In the case the variable has two boundary points, it means that the domain value of the variable is in an interval such as [x, y], we need to test this variable with only 7 values possible: x, x-1, x+1, y, y-1, y+1 and a random value which is far from x and y but in the interval [x, y].
The same principle for the case there are more than two boundary points.
Note that, in some case, we do not need to test the case out of rage of the variable.

Applying

In applying the boundary test strategy into the case of the method sum. We have two variable a and b. Each has an interval value from -2^32 to 2^31. In this case, we do not need to test with the case out of range value of a and b. So, for each variable, we have to test with these following values:
– -2^32
– -2^32+1
– a random value in the valid range, for instance 100
– 2^31-1
– 2^31
So we have totally 5^2 = 25 test cases.
Note that, we could put all these testcases into a method in the test class or separate them into 25 different methods. In this tutorial, we regroup them into 5 methods corresponding to 5 test values of a, each method will test with 5 values of b.

Code complete

Util.java

public class Util {	
	public Util(){		
	}		
	public long sum(int a, int b){
		return (long)a+b;
	}
}

TestUtil.java

import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestUtil {
	@Test
	public void testSum1() {	
		Util cal = new Util();
		assertEquals(-4294967296l,cal.sum(-2147483648,-2147483648));
		assertEquals(-4294967295l,cal.sum(-2147483648,-2147483647));
		assertEquals(-2147483548,cal.sum(-2147483648,100));
		assertEquals(-2,cal.sum(-2147483648,2147483646));
		assertEquals(-1,cal.sum(-2147483648,2147483647));
	}
	@Test
	public void testSum2() {	
		Util cal = new Util();
		assertEquals(-4294967295l,cal.sum(-2147483647,-2147483648));
		assertEquals(-4294967294l,cal.sum(-2147483647,-2147483647));
		assertEquals(-2147483547,cal.sum(-2147483647,100));
		assertEquals(-1,cal.sum(-2147483647,2147483646));
		assertEquals(-0,cal.sum(-2147483647,2147483647));
	}
	@Test
	public void testSum3() {	
		Util cal = new Util();
		assertEquals(-2147483548,cal.sum(100,-2147483648));
		assertEquals(-2147483547,cal.sum(100,-2147483647));
		assertEquals(200,cal.sum(100,100));
		assertEquals(2147483746l,cal.sum(100,2147483646));
		assertEquals(2147483747l,cal.sum(100,2147483647));
	}
	@Test
	public void testSum4() {	
		Util cal = new Util();
		assertEquals(-2,cal.sum(2147483646,-2147483648));
		assertEquals(-1,cal.sum(2147483646,-2147483647));
		assertEquals(2147483746l,cal.sum(2147483646,100));
		assertEquals(4294967292l,cal.sum(2147483646,2147483646));
		assertEquals(4294967293l,cal.sum(2147483646,2147483647));
	}
	@Test
	public void testSum5() {	
		Util cal = new Util();
		assertEquals(-1,cal.sum(2147483647,-2147483648));
		assertEquals(0,cal.sum(2147483647,-2147483647));
		assertEquals(2147483747l,cal.sum(2147483647,100));
		assertEquals(4294967293l,cal.sum(2147483647,2147483646));
		assertEquals(4294967294l,cal.sum(2147483647,2147483647));
	}
}

Run JUnit test

In Eclipse, right click on the TestUtil.java, select run JUnit. The result is displayed as follow:

Leave a comment