CategoryNeedProofRead NB 6.0 includes some JUnit 4 support, so this page should be revised accordingly.

Can I use JUnit 4 to run tests in my project?

Yes you can. The IDE bundles JUnit 3.8.2 and does not include direct support for the new release. But it is still possible if you don't mind doing a little extra setup, and can live without the JUnit Results Window.

First, download


and unpack to some location on disk. For purposes of this example, let's say you picked /opt/junit4.1 as your installation directory.

Go to Tools | Library Manager, click New Library, and name it junit4. The settings should be as follows:

Classpath: /opt/junit4.1/junit-4.1.jar
Sources:   /opt/junit4.1/junit-4.1-src.jar
Javadoc:   /opt/junit4.1/javadoc

Now you are ready to use this library in a project. As an example, create the Anagram Game sample project. We will upgrade its test to use JUnit 4.

First, open nbproject/project.properties from the Files tab. Find where it says


and simply change it to


If you look under Test Libraries in the Projects tab you should see the change take effect. Note that since JUnit 4 is backward compatible with JUnit 3, it is safe to do this for any project; you can still run old-style tests without any further changes.

Next we will upgrade the project's test to use the new style. Open WordLibraryTest.java and edit it to look like this (significant changes marked):

package com.toy.anagrams.lib;
import java.util.Arrays;
import static org.junit.Assert.*; // ADDED
import org.junit.Test;            // EDITED
public class WordLibraryTest {
    @Test()                       // ADDED
    public void isCorrect() {     // EDITED
        for (int i = 0; i < WordLibrary.getSize(); i++) {
            String clearWord = WordLibrary.getWord(i);
            String scrambledWord = WordLibrary.getScrambledWord(i);
            assertTrue("Scrambled word \"" + scrambledWord +
                       "\" at index: " + i +
                       " does not represent the word \"" + clearWord + "\"",
                       isAnagram(clearWord, scrambledWord));
    private boolean isAnagram(String clearWord, String scrambledWord) {
        char[] clearArray = clearWord.toCharArray();
        char[] scrambledArray = scrambledWord.toCharArray();
        return Arrays.equals(clearArray, scrambledArray);

You should open the Properties dialog for this project and under Sources change Source Level to 1.5 since we are using annotations and a static import.

The new test will compile fine – the IDE can offer accurate code completion and so on. But we cannot run it yet, since the IDE's standard test running framework (based on Ant 1.6.5) does not understand JUnit 4 tests.

We can remedy this (at the loss of the JUnit Results Window). Open build.xml and add:

    <target name="-init-macrodef-junit">
        <macrodef name="junit" uri="http://www.netbeans.org/ns/j2se-project/3">
            <attribute name="includes" default="**/*Test.java"/>
                <property name="test.src.dir.absolute" location="${test.src.dir}"/>
                <pathconvert property="classes" pathsep=" " dirsep=".">
                        <fileset dir="${test.src.dir.absolute}" includes="@{includes}"/>
                    <mapper type="glob" from="${test.src.dir.absolute}${file.separator}*.java" to="*"/>
                <java classname="org.junit.runner.JUnitCore" fork="true"
                               dir="${basedir}" failonerror="false" resultproperty="junit.result">
                    <arg line="${classes}"/>
                        <path path="${run.test.classpath}"/>
                        <propertyref prefix="test-sys-prop."/>
                        <mapper type="glob" from="test-sys-prop.*" to="*"/>
                    <jvmarg line="${run.jvmargs}"/>
                <condition property="tests.failed">
                        <equals arg1="${junit.result}" arg2="0"/>
    <target name="-debug-start-debuggee-test" if="have.tests"
                     depends="init,compile-test" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3">
        <fail unless="test.class">Must select one file in the IDE or set test.class</fail>
        <j2seproject3:debug classname="org.junit.runner.JUnitCore" classpath="${debug.test.classpath}">
                <arg line="${test.class}"/>

Now you should be able to use Test Project, Run File on one or more tests, and Debug File on a test (or Debug Test for File on the tested class).

In case one really wishes to see the JUnit Results Window there is another trick to integrate with JUnit 4 – one can just add a suite method to each class and thus provide a bridge that JUnit 3.8 understands. Just add:

public static junit.framework.Test suite() {
    return new JUnit4TestAdapter(WordLibraryTest.class);

and the existing NetBeans 5.0 JUnit integration will find and recognize the test written in JUnit 4.

Applies to: NetBeans 5.0

Apache Ant 1.7 is expected to support JUnit 4.x to a limited extent. There are also plans to provide direct JUnit 4.x support in a future version of NetBeans:


Looking for TestNG support? TestNG is another new unit test framework for Java competing with JUnit 4.x. Experimental support for NetBeans is available here:


Another approach for TestNG or other test runners: "NetBeans and Alternative Test Runners: Customizing Standard Java Projects" (abstract, or article). Like the JUnit4 approach on this page, the article customizes the existing NetBeans commands (Debug Test File, Run Test File, Test Project), so it works on the project tests, but with other test runners. Unlike the experimental plugin, it does not require you to use separate panes and commands. The article explains how it works so you can recreate the behavior for any test runner. To use the given examples for TestNG or SuiteRunner, jump to the import section, which simply adds 1 import to your build file.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo