NetBeans Create JUnit Test Suite Example Guide

You may be here because you are new to Netbeans and would like to know how to run Junit test cases in Netbeans as a test Suite. Often, it would be nice to group your JUnit test cases into a test Suite so that they can all be executed in one go, instead of triggering them one after another. To do this, you will have to create a Suite that points to the test cases (class files) that needs to be executed, and if you are using Netbeans, here is a step by step guide to define a test Suite in Netbeans to run your JUnit test cases. I have a Java project which contains two JUnit test class files for testing same methods, but they use different type of runners to run the test execution. To define a test Suite that combines both these class files into a suite, I followed the instructions as shown below in Netbeans;
1) Right Click your Project Name -> Select New -> Click on Other. When you do this, you will get a screen as shown below;
Create a JUnit Test Suite in Netbeans
Creating a JUnit Test Suite Using Netbeans
2) Make sure you select Junit and then select "Test Suite" under file types. Once you do this, you have to click on the Next button in the screen.

3) Under "Name and Location", enter "JUnitTestSuite" as your Class Name. Under "Generated Code" uncheck Test initializer and Test Finalizer Checkboxes and hit the Finish button on the page.

4) Netbeans will automatically create a test suite class file. A sample of the Suite file is shown below (this may differ for you depending on your test input files)
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({AdditionTestParametrized.class, AdditionTest.class})
public class JUnitTestSuite {

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
}
You may wish to change "@Suite.SuiteClasses" to include only specific files in your test suite. In my case, I had two JUnit test class files and Netbeans included both of them in the Suite file. Once you do this, you can run the Suite file which will automatically run the individual test unit files.

1 comment:

  1. excellent article. But I need more written

    ReplyDelete