Make everything as simple as possible, but not simpler. -Albert Einstein 

formats

Unit Testing with JUnit 4.x

Published on April 22, 2012 by in JUnit

Summary: JUnit is the standard unit testing framework for Java.  JUnit 4.x further reduces the complexity for unit testing by utilizing annotations.  As a result, software developers are ‘freed’  from inheriting from specific classes as well as applying specific naming conventions to methods. This post will describe how to make use of some of the key features within JUnit 4.x.

Prerequisites: If you would like to obtain this article’s complete sample, it may be obtained from our GitHub repository.  All samples are Maven based java projects.

JUnit Testing: JUnit 4.x uses annotations to select methods that have been chosen for unit tests.  JUnit  will invoke ‘annotated’ methods in no predetermined order. As a result, your test should be atomic and not rely on other test methods.

In order to write a JUnit 4.x test, perform the following:

1.  Annotate the method with @org.junit.Test

2.  Utilize one of  JUnit’s org.junit.Assert methods to verify expected result with actual result.

Let’s Get Started: Listing 1 displays MyMath.java which contains 2 methods: add(x,y) and multiply(x,y).  The following sections will demonstrate how to unit test these methods with JUnit 4.x.

Listing 1: MyMath.java

package techbysample.junit4.sample1;

/**
*
* @author TechBySample.com
*
*/

public class MyMath {

public int add(int a, int b) {
return a+b;
}
public int multiply(int a, int b)
{
return a*b;
}
}

In Listing 2,  We implement MyMathTest.java to apply the steps in involved in writing a JUnit 4.x testcase.  Notice that MyMathTest.java does not extend any other class with JUnit.  In addition, @org.junit.Test annotations are applied to testAdd() and testMultiply() to unit test MyMath.java’s add(x,y) and multiply(x,y) methods respectively.  @org.junit.Before is applied to MyMathTest.java’s initialize() method to tell  JUnit to invoke this method prior to unit testing each method.  @org.junit.After is applied to MyMathTest.java’s tearDown() method to tell JUnit to invoke this method immediately after testing each method.

Listing 2: MyMathTest.java

package techbysample.junit4.sample1;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
*
* @author TechBySample.com
*
*/

public class MyMathTest {

private MyMath myMath = null;

@Before
public void initialize() {

System.out.println("@Before- initialize()");
myMath = new MyMath();
}

@Test
public void testAdd()
{
System.out.println("@Test- testAdd()");
int x=1;
int y=2;

int z = myMath.add(x, y);
Assert.assertEquals(3, z);
}

@Test
public void testMultiply()
{
System.out.println("@Test- testMultiply()");
int x=3;
int y=2;

int z = myMath.multiply(x, y);
Assert.assertEquals(6, z);
}

@After
public void tearDown()
{
System.out.println("@After- tearDown()");

}
}

Executing a JUnit4.x test: There are many ways to run a JUnit4.x testcase. We elect to make use of the built in features within Maven to invoke our test.                                  From the ‘junit4-sample1’ project directory, type:

mvn -Dtest=MyMathTest test

You should see the following output:

JUnit4_Sample1

Resources:
Official JUnit site
Official TechBySample GitHub repository

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Techbysample.com, all rights reserved.