JUnit Installation Guideline
JUnit Installation Guideline
To complete this tutorial you first create a Java class library project called JUnit-Sample. After
you create the project, you copy two classes from the sample project JUnitSampleSol to your
project JUnit-Sample.
For this tutorial there is little reason to copy project libraries to a dedicated folder because
you will not need to share libraries with other users or projects.
Click Finish.
The first time that you create a JUnit test the IDE prompts you to select a version and then adds a
Test Libraries node and the JUnit library.
You can download the sample project JUnitSampleSol used in this tutorial in the following
ways.
When you click Finish, the IDE initializes the local folder as a Subversion
repository and checks out the project sources.
6. Click Open Project in the dialog that appears when checkout is complete.
For more about installing Subversion, see the section on Setting up Subversion in the
Guide to Subversion in NetBeans IDE.
Note. If you did not install the JUnit plugin when you installed the IDE, when you open the
NetBeans project you will be prompted to install the JUnit plugin to resolve the reference to the
JUnit libraries.
In this exercise you copy the files Utils.java and Vectors.java from the sample project
JUnitSampleSol into the class library project that you created.
1. In the Projects window, right-click the Source Packages node of the JUnit-Sample
project and choose New > Java Package from the popup menu.
2. Type sample as the package name. Click Finish.
3. Open the JUnitSampleSol project (if not already open) and expand the Source Packages
node in the Projects window.
4. Copy the classes Utils.java and Vectors.java in the JUnitSampleSol project and
paste them into the sample source package in JUnit-Sample.
If you look at the source code for the classes, you can see that Utils.java has three methods
(computeFactorial, concatWords, and normalizeWord) and that Vectors.java has two
methods (equal and scalarMultiplication). The next step is to create test classes for each
class and write some test cases for the methods.
Note. You can close the JUnitSampleSol project because you will not need it again. The
JUnitSampleSol project contains all the tests described in this document.
In this exercise you create a JUnit test skeleton for Vectors.java. You will also select JUnit as
the test framework and JUnit 3 as the version.
Note. If you are using NetBeans IDE 7.1 or earlier you do not need to specify the test framework
because JUnit is specified by default. In NetBeans IDE 7.2 you have the option of specifying
JUnit or TestNG as the test framework.
When you change the name of the test class, you will see a warning about changing the
name. The default name is based on the name of the class you are testing, with the word
Test appended to the name. For example, for the class MyClass.java, the default name
of the test class is MyClassTest.java. Usually it is best to keep the default name, but for
this tutorial you will change the name because you will also create JUnit 4 tests in the
same package and the names of the test classes must be unique.
When you select JUnit 3.x the IDE adds the JUnit 3 library to the project.
When you click Select, the IDE creates the VectorsJUnit3Test.java test class in the
sample package under the Test Packages node in the Projects window.
A project requires a directory for test packages to create tests. The default location for the
test packages directory is at the root level of the project, but depending on the type of
project you can specify a different location for the directory in the project's Properties
dialog.
If you look at the generated test class VectorsJUnit3Test.java in the editor, you can
see that the IDE generated the following test class with test methods for the methods
equal and scalarMultiplication.
/**
* Test of scalarMultiplication method, of class Vectors.
*/
public void testScalarMultiplication() {
System.out.println("scalarMultiplication");
int[] a = null;
int[] b = null;
int expResult = 0;
int result = Vectors.scalarMultiplication(a, b);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default
call to fail.
fail("The test case is a prototype.");
}
}
The method body of each generated test is provided solely as a guide and needs to be modified to
be an actual test case. You can deselect Default Method Bodies in the Create Tests dialog if you
do not want the code generated for you.
When the IDE generates the names for the test methods, each method name is prepended with
test because JUnit 3 uses naming conventions and reflection to identify tests. To identify test
methods, each test method is required to follow the syntax test<NAME>.
Note. In JUnit 4 it is no longer necessary to use this test method naming syntax because you can
use annotations to identify test methods and the test class is no longer required to extend
TestCase.
In this exercise you modify the generated test methods to make them functioning tests and
modify the default output messages. You do not need to modify the output messages to run the
tests, but you may want to modify the output to help identify the results displayed in the JUnit
Test Results output window.
This test method uses the JUnit assertEquals method. To use the assertion, you supply
the input variables and the expected result. To pass the test, the test method must
successfully return all the expected results based on the supplied variables when running
the tested method. You should add a sufficient number of assertions to cover the various
possible permutations.
14. Modify the test skeleton for testEqual by deleting the generated method bodies and
adding the following println.
15. Modify the testEqual method by adding the following assertions (displayed in bold).
16. public void testEqual() {
17. System.out.println("* VectorsJUnit3Test: testEqual()");
18. assertTrue(Vectors.equal(new int[] {}, new int[] {}));
19. assertTrue(Vectors.equal(new int[] {0}, new int[] {0}));
20. assertTrue(Vectors.equal(new int[] {0, 0}, new int[] {0, 0}));
21. assertTrue(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0,
0}));
22. assertTrue(Vectors.equal(new int[] {5, 6, 7}, new int[] {5, 6,
7}));
23.
24. assertFalse(Vectors.equal(new int[] {}, new int[] {0}));
25. assertFalse(Vectors.equal(new int[] {0}, new int[] {0, 0}));
26. assertFalse(Vectors.equal(new int[] {0, 0}, new int[] {0, 0, 0}));
27. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0}));
28. assertFalse(Vectors.equal(new int[] {0, 0}, new int[] {0}));
29. assertFalse(Vectors.equal(new int[] {0}, new int[] {}));
30.
31. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0,
1}));
32. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 1,
0}));
33. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {1, 0,
0}));
34. assertFalse(Vectors.equal(new int[] {0, 0, 1}, new int[] {0, 0,
3}));
}
This test uses the JUnit assertTrue and assertFalse methods to test a variety of
possible results. For the test of this method to pass, the assertTrue must all be true and
assertFalse must all be false.
35. Save your changes.
You now create the test skeletons for Utils.java. When you created the test in the previous
exercise, the IDE prompted you for the version of JUnit. You are not prompted to select a
version this time.
When you click OK, the IDE creates the test file UtilsJUnit3Test.java in the Test Packages >
samples directory. You can see that in addition to creating the test skeletons
testComputeFactorial, testConcatWords, and testNormalizeWord for the methods in
Utils.java, the IDE also creates the test initializer method setUp and the test finalizer method
tearDown.
In this exercise you add some test cases that illustrate some common JUnit test elements. You
also add a println to the methods because some methods do not print any output by default. By
adding a println to the methods you can later look in the JUnit test result window to see if the
methods were run and the order in which they were run.
The setUp and tearDown methods are used to initialize and finalize test conditions. You do not
need the setUp and tearDown methods to test Utils.java, but they are included here to
demonstrate how they work.
The setUp method is a test initialization method and is run before each test case in the test class.
A test initialization method is not required for running tests, but if you need to initialize some
variables before you run a test, you use the test initializer method.
The tearDown method is a test finalizer method and is run after each test case in the test class. A
test finalizer method is not required for running tests, but you may need a finalizer to clean up
any data that was required when running the test cases.
1. Make the following changes (displayed in bold) to add a println to each method.
2. @Override
3. protected void setUp() throws Exception {
4. super.setUp();
5. System.out.println("* UtilsJUnit3Test: setUp() method");
6. }
7.
8. @Override
9. protected void tearDown() throws Exception {
10. super.tearDown();
11. System.out.println("* UtilsJUnit3Test: tearDown() method");
}
When you run the test the println text for each methods will appear in the JUnit Test
Results output window. If you do not add the println, there is no output to show that the
methods were run.
This simple test case tests the concatWords method. Instead of using the generated test method
testConcatWords, you will add a new test method called testHelloWorld that uses a single
simple assertion to test if the method concatenates the strings correctly. The assertEquals in
the test case uses the syntax assertEquals(EXPECTED_RESULT, ACTUAL_RESULT) to test if the
expected result is equal to the actual result. In this case, if the input to the method concatWords
is "Hello", ", ", "world" and "!", the expected result should equal "Hello, world!".
5. Add a println statement to display text about the test in the JUnit Test Results window.
6. public void testHelloWorld() {
7. System.out.println("* UtilsJUnit3Test: test method 1 -
testHelloWorld()");
assertEquals("Hello, world!", Utils.concatWords("Hello", ", ",
"world", "!"));
This test demonstrates how to check if a method is taking too long to complete. If the method is
taking too long, the test thread is interrupted and the test fails. You can specify the time limit in
the test.
The test method invokes the computeFactorial method in Utils.java. You can assume that
the computeFactorial method is correct, but in this case you want to test if the computation is
completed within 1000 milliseconds. The computeFactorial thread and a test thread are started
at the same time. The test thread will stop after 1000 milliseconds and throw a
TimeoutException unless the computeFactorial thread completes first. You will add a
message so that a message is displayed if a TimeoutException is thrown.
You can modify the Thread.sleep line to change the number of milliseconds before the
timeout is thrown.
27. Add the following println (displayed in bold) to print the text about the test in the JUnit
Test Results window.
28. public void testWithTimeout() throws InterruptedException,
TimeoutException {
29. System.out.println("* UtilsJUnit3Test: test method 2 -
testWithTimeout()");
30. final int factorialOf = 1 + (int) (30000 * Math.random());
31. System.out.println("computing " + factorialOf + '!');
Compare: Testing Using a Timeout (JUnit 4)
This test demonstrates how to test for an expected exception. The method fails if it does not
throw the specified expected exception. In this case you are testing that the computeFactorial
method throws an IllegalArgumentException if the input variable is a negative number (-5).
9. Add the following println (displayed in bold) to print the text about the test in the JUnit
Test Results window.
10. public void testExpectedException() {
11. System.out.println("* UtilsJUnit3Test: test method 3 -
testExpectedException()");
try {
Disabling a Test
This test demonstrates how to temporarily disable a test method. In JUnit 3, if a method name
does not start with test it is not recognized as a test method. In this case you prepend
DISABLED_ to the name of the test method to disable it.
The test method testTemporarilyDisabled will run if you run the test class.
Now that you have written the tests, you can run the test and see the test output in the JUnit Test
Results window.
When you run a JUnit test the results are displayed in the Test Results window of the IDE. You
can run individual JUnit test classes or you can choose Run > Test PROJECT_NAME from the
main menu to run all the tests for the project. If you choose Run > Test, the IDE runs all the test
classes in the Test Packages folder. To run an individual test class, right-click the test class under
the Test Packages node and choose Run File.
1. Choose Run > Set Main Project in the main menu and select the JUnit-Sample project.
2. Choose Run > Test Project (JUnit-Sample) from the main menu.
3. Choose Window > IDE Tools > Test Results to open the Test Results window.
When you run the test you will see one of the following results in the JUnit Test Results window.
In this image (click the image to see a larger image) you can see that the project passed all the
tests. The left pane displays the results of the individual test methods and the right pane displays
the test output. If you look at the output you can see the order that the tests were run. The
println that you added to each of the test methods printed out the name of the test to the output
window. You can also see that in UtilJUnit3Test the setUp method was run before each test
method and the tearDown method was run after each method.
In this image (click the image to see a larger image) you can see that the project failed one of the
tests. The testTimeout method took too long to complete and the test thread was interrupted,
causing that test to fail. It took longer than 1000 milliseconds to compute the factorial of the
randomly generated number (22991).
The next step after you create your unit test classes is to create test suites. See Creating JUnit 3
Test Suites to see how to run specified tests as a group so you do not have to run each test
individually.
You will use the IDE's wizards to create test skeletons based on the classes in your project. The
first time that you use the IDE to create some test skeletons for you, the IDE prompts you to
choose the JUnit version.
Note. If you already selected JUnit 3.x as the default version for your tests, you need to change
the default version to JUnit 4.x. To change the default JUnit version, expand the Test Libraries
node, right-click the JUnit library and choose Remove. You can now use the Add Library dialog
box to explicitly add the JUnit 4 library or you can select version 4.x when you are prompted to
select the JUnit version when you create a new test. You can still run JUnit 3 tests, but any new
tests you create will use JUnit 4.
In this exercise you will create the JUnit test skeletons for Vectors.java.
Note. If you are using NetBeans IDE 7.1 or earlier you do not need to specify the test framework
because JUnit is specified by default. In NetBeans IDE 7.2 you have the option of specifying
JUnit or TestNG as the test framework.
When you change the name of the test class, you will see a warning about changing the
name. The default name is based on the name of the class you are testing, with the word
Test appended to the name. For example, for the class MyClass.java, the default name
of the test class is MyClassTest.java. Unlike JUnit 3, in JUnit 4, test are not required to
end with the word Test. Usually it is best to keep the default name, but because you are
creating all the JUnit tests in the same package in this tutorial the names of the test
classes have to be unique.
5. Select JUnit 4.x in the Select JUnit Version dialog box. Click Select.
When you click OK, the IDE creates the VectorsJUnit4Test.java test class in the sample
package under the Test Packages node in the Projects window.
Note. A project requires a directory for test packages to create tests. The default location for the
test packages directory is at the root level of the project, but you can specify a different location
for the directory in the project's Properties dialog.
If you look at VectorsJUnit3Test.java in the editor, you can see that the IDE generated the
test methods testEqual and testScalarMultiplication. In VectorsJUnit4Test.java, each
test method is annotated with @Test. The IDE generated the names for the test methods based on
the names of the method in Vectors.java but the name of the test method is not required to
have test prepended. The default body of each generated test method is provided solely as a
guide and needs to be modified to be actual test cases.
You can deselect Default Method Bodies in the Create Tests dialog if you do not want the bodies
of the method generated for you.
The IDE also generated the following test class initializer and finalizer methods:
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
The IDE generates the class initializer and finalizer methods by default when creating JUnit 4
test classes. The annotations @BeforeClass and @AfterClass are used to mark methods that
should be run before and after running the test class. You can delete the methods because you
will not need them to test Vectors.java.
You can configure the methods that are generated by default by configuring the JUnit options in
the Options window.
Note. For JUnit 4 tests, notice that by default the IDE adds a static import declaration for
org.junit.Assert.*.
In this exercise you modify each of the generated test methods to test the methods using the
JUnit assert method and to change the names of the test methods. In JUnit 4 you have greater
flexibility when naming test methods because test methods are indicated by the @Test annotation
and do not require the word test prepended to test method names.
Note. When writing tests it is not necessary to change the printed output. You do this in
this exercise so that it is easier to identify the test results in the output window.
In this test method you use the JUnit assertEquals method. To use the assertion, you
supply the input variables and the expected result. To pass the test, the test method must
successfully return all the expected results based on the supplied variables when running
the tested method. You should add a sufficient number of assertions to cover the various
possible permutations.
16. Change the name of the testEqual test method to equalsCheck.
17. Delete the the generated method body of the equalsCheck test method.
18. Add the following println to the equalsCheck test method.
@Test
public void equalsCheck() {
System.out.println("* VectorsJUnit4Test: equalsCheck()");
}
19. Modify the equalsCheck method by adding the following assertions (displayed in bold).
20. @Test
21. public void equalsCheck() {
22. System.out.println("* VectorsJUnit4Test: equalsCheck()");
23. assertTrue(Vectors.equal(new int[] {}, new int[] {}));
24. assertTrue(Vectors.equal(new int[] {0}, new int[] {0}));
25. assertTrue(Vectors.equal(new int[] {0, 0}, new int[] {0, 0}));
26. assertTrue(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0,
0}));
27. assertTrue(Vectors.equal(new int[] {5, 6, 7}, new int[] {5, 6,
7}));
28.
29. assertFalse(Vectors.equal(new int[] {}, new int[] {0}));
30. assertFalse(Vectors.equal(new int[] {0}, new int[] {0, 0}));
31. assertFalse(Vectors.equal(new int[] {0, 0}, new int[] {0, 0, 0}));
32. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0}));
33. assertFalse(Vectors.equal(new int[] {0, 0}, new int[] {0}));
34. assertFalse(Vectors.equal(new int[] {0}, new int[] {}));
35.
36. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0,
1}));
37. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 1,
0}));
38. assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {1, 0,
0}));
39. assertFalse(Vectors.equal(new int[] {0, 0, 1}, new int[] {0, 0,
3}));
}
This test uses the JUnit assertTrue and assertFalse methods to test a variety of
possible results. For the test of this method to pass, the assertTrue must all be true and
assertFalse must all be false.
You will now create the JUnit test methods for Utils.java. When you created the test class in
the previous exercise, the IDE prompted you for the version of JUnit. You are not prompted to
select a version this time because you already selected the JUnit version and all subsequent JUnit
tests are created in that version.
Note. You can still write and run JUnit 3 tests if you select JUnit 4 as the version, but the IDE
uses the JUnit 4 template for generating test skeletons.
When you click OK, the IDE creates the test file UtilsJUnit4Test.java in the Test Packages >
sample directory. You can see that the IDE generated the test methods testComputeFactorial,
testConcatWords, and testNormalizeWord for the methods in Utils.java. The IDE also
generated initializer and finalizer methods for the test and the test class.
In this exercise you will add test cases that illustrate some common JUnit test elements. You will
also add a println to the methods because some methods do not print any output to the JUnit
Test Results window to indicate that they were run, or to indicate that the method passed the test.
By adding a println to the methods you can see if the methods were run and the order in which
they were run.
When you created the test class for Utils.java the IDE generated annotated initializer and
finalizer methods. You can choose any name for the name of the method because there is no
required naming convention.
Note. You do not need the initializer and finalizer methods to test Utils.java, but they are
included in this tutorial to demonstrate how they work.
In JUnit 4 you can use annotations to mark the following types of initializer and finalizer
methods.
Test Class Initializer. The @BeforeClass annotation marks a method as a test class
initialization method. A test class initialization method is run only once, and before any
of the other methods in the test class. For example, instead of creating a database
connection in a test initializer and creating a new connection before each test method,
you may want to use a test class initializer to open a connection before running the tests.
You could then close the connection with the test class finalizer.
Test Class Finalizer. The @AfterClass annotation marks a method as a test class
finalizer method. A test class finalizer method is run only once, and after all of the other
methods in the test class are finished.
Test Initializer. The @Before annotation marks a method as a test initialization method.
A test initialization method is run before each test case in the test class. A test
initialization method is not required to run tests, but if you need to initialize some
variables before you run a test, you use a test initializer method.
Test Finalizer. The @After annotation marks a method as a test finalizer method. A test
finalizer method is run after each test case in the test class. A test finalizer method is not
required to run tests, but you may need a finalizer to clean up any data that was required
when running the test cases.
@BeforeClass
public static void setUpClass() throws Exception {
System.out.println("* UtilsJUnit4Test: @BeforeClass method");
}
@AfterClass
public static void tearDownClass() throws Exception {
System.out.println("* UtilsJUnit4Test: @AfterClass method");
}
@Before
public void setUp() {
System.out.println("* UtilsJUnit4Test: @Before method");
}
@After
public void tearDown() {
System.out.println("* UtilsJUnit4Test: @After method");
}
When you run the test class the println text you added is displayed in the output pane of the
JUnit Test Results window. If you do not add the println, there is no output to indicate that the
initializer and finalizer methods were run.
This simple test case tests the concatWords method. Instead of using the generated test method
testConcatWords, you will add a new test method called helloWorldCheck that uses a single
simple assertion to test if the method concatenates the strings correctly. The assertEquals in
the test case uses the syntax assertEquals(EXPECTED_RESULT, ACTUAL_RESULT) to test if the
expected result is equal to the actual result. In this case, if the input to the method concatWords
is "Hello", ",", "world" and "!", the expected result should equal "Hello, world!".
6. Add a println statement to display text about the test in the JUnit Test Results window.
7. @Test
8. public void helloWorldCheck() {
9. System.out.println("* UtilsJUnit4Test: test method 1 -
helloWorldCheck()");
assertEquals("Hello, world!", Utils.concatWords("Hello", ", ",
"world", "!"));
This test demonstrates how to check if a method is taking too long to complete. If the method is
taking too long, the test thread is interrupted and the test fails. You can specify the time limit in
the test.
The test method invokes the computeFactorial method in Utils.java. You can assume that
the computeFactorial method is correct, but in this case you want to test if the computation is
completed within 1000 milliseconds. You do this by interrupting the test thread after 1000
milliseconds. If the thread is interrupted the test method throws a TimeoutException.
8. Add the following code (displayed in bold) to set the timeout and to interrupt the thread if
the method takes too long to execute.
9. @Test(timeout=1000)
10. public void testWithTimeout() {
final int factorialOf = 1 + (int) (30000 * Math.random());
11. Add the following println (displayed in bold) to print the text about the test in the JUnit
Test Results window.
12. @Test(timeout=1000)
13. public void testWithTimeout() {
14. System.out.println("* UtilsJUnit4Test: test method 2 -
testWithTimeout()");
15. final int factorialOf = 1 + (int) (30000 * Math.random());
16. System.out.println("computing " + factorialOf + '!');
This test demonstrates how to test for an expected exception. The method fails if it does not
throw the specified expected exception. In this case you are testing that the computeFactorial
method throws an IllegalArgumentException if the input variable is a negative number (-5).
6. Add the following property (displayed in bold) to the @Test annotation to specify that the
test is expected to throw IllegalArgumentException.
7. @Test(expected=IllegalArgumentException.class)
8. public void checkExpectedException() {
9. final int factorialOf = -5;
10. System.out.println(factorialOf + "! = " +
Utils.computeFactorial(factorialOf));
}
11. Add the following println (displayed in bold) to print the text about the test in the JUnit
Test Results window.
12. @Test (expected=IllegalArgumentException.class)
13. public void checkExpectedException() {
14. System.out.println("* UtilsJUnit4Test: test method 3 -
checkExpectedException()");
15. final int factorialOf = -5;
16. System.out.println(factorialOf + "! = " +
Utils.computeFactorial(factorialOf));
}
Disabling a Test
This test demonstrates how to temporarily disable a test method. In JUnit 4 you simply add the
@Ignore annotation to disable the test.
The test method temporarilyDisabledTest will run if you run the test class.
7. Add the @Ignore annotation (displayed in bold) above @Test to disable the test.
8. @Ignore
9. @Test
10. public void temporarilyDisabledTest() throws Exception {
11. System.out.println("* UtilsJUnit4Test: test method 4 -
checkExpectedException()");
12. assertEquals("Malm\u00f6", Utils.normalizeWord("Malmo\u0308"));
}
Now that you have written the tests you can run the test and see the test output in the JUnit Test
Results window.
You can run JUnit tests on the entire application or on individual files and see the results in the
IDE. The easiest way to run all the unit tests for the project is to choose Run > Test
<PROJECT_NAME> from the main menu. If you choose this method, the IDE runs all the test
classes in the Test Packages. To run an individual test class, right-click the test class under the
Test Packages node and choose Run File.
When you run UtilsJUnit4Test.java the IDE only runs the tests in the test class. If the class
passes all the tests you will see something similar to the following image in the JUnit Test
Results window.
In this image (click the image to see a larger image) you can see that the IDE ran the JUnit test
on Utils.java and that the class passed all the tests. The left pane displays the results of the
individual test methods and the right pane displays the test output. If you look at the output you
can see the order that the tests were run. The println that you added to each of the test methods
printed out the name of the test to Test Results window and the Output window.
You can see that in UtilsJUnit4Test the test class initializer method annotated with
@BeforeClass was run before any of the other methods and it was run only once. The test class
finalizer method annotated with @AfterClass was run last, after all the other methods in the
class. The test initializer method annotated with @Before was run before each test method.
The controls in the left side of the Test Results window enable you to easily run the test again.
You can use the filter to toggle between displaying all test results or only the failed tests. The
arrows enable you to skip to the next failure or the previous failure.
When you right-click a test result in the Test Results window, the popup menu enables you to
choose to go to the test's source, run the test again or debug the test.
The next step after creating your unit test classes is to create test suites. See Creating JUnit 4
Test Suites to see how to run specified tests as a group so you do not have to run each test
individually.
A test suite is basically a class with a method that invokes the specified test cases, such as
specific test classes, test methods in test classes and other test suites. A test suite can be included
as part of a test class but best practices recommends creating individual test suite classes.
You can create JUnit 3 and JUnit 4 test suites for your project manually or the IDE can generate
the suites for you. When you use the IDE to generate a test suite, by default the IDE generates
code to invoke all the test classes in the same package as the test suite. After the test suite is
created you can modify the class to specify the tests you want to run as part of that suite.
If you selected JUnit 3 as the version for your tests, the IDE can generate JUnit 3 test suites
based on the test classes in the test package. In JUnit 3 you specify the test classes to include in
the test suite by creating an instance of TestSuite and using the addTest method for each test.
1. Right-click the JUnit-Sample project node in the Projects window and choose New >
Other to open the New File wizard.
2. Select Test Suite in the Unit Tests category. Click Next.
3. Type JUnit3TestSuite for the Class Name.
4. Select the sample package to create the test suite in the sample folder in the test packages
folder.
5. Deselect Test Initializer and Test Finalizer. Click Finish.
When you click Finish, the IDE creates the test suite class in the sample package and
opens the class in the editor. The test suite will contain the following code.
If you selected JUnit 4 for the version of your tests, the IDE can generate JUnit 4 test suites.
JUnit 4 is back-compatible so you can run JUnit 4 test suites that contain JUnit 4 and JUnit 3
tests. In JUnit 4 test suites you specify the test classes to include as values of the @Suite
annotation.
Note. To run JUnit 3 test suites as part of a JUnit 4 test suite requires JUnit 4.4 or higher.
1. Right-click the project node in the Projects window and choose New > Other to open the
New File wizard.
2. Select Test Suite in the Unit Tests category. Click Next.
3. Type JUnit4TestSuite for the file name.
4. Select the sample package to create the test suite in the sample folder in the test packages
folder.
5. Deselect Test Initializer and Test Finalizer. Click Finish.
When you click Finish, the IDE creates the test suite class in the sample package and opens the
class in the editor. The test suite contains code similar to the following.
@RunWith(Suite.class)
@Suite.SuiteClasses(value={UtilsJUnit4Test.class, VectorsJUnit4Test.class})
public class JUnit4TestSuite {
}
When you run the test suite the IDE will run the test classes in the order that they are listed.
You run a test suite the same way you run any individual test class.
In this image (click the image to see a larger image) you can see the test results for a JUnit 3 test
suite. The test suite ran the UtilsJUnit3Test and VectorsJUnit3Test test classes as a single
test and displayed the test results in the left pane as the results of a single test. The output in the
right pane is the same as when you run the test individually.
In this image (click the image to see a larger image) you can see the test results for a JUnit 4 test
suite. The test suite ran the UtilsJUnit4Test and VectorsJUnit4Test test classes as a single
test and displayed the test results in the left pane as the results of a single test. The output in the
right pane is the same as when you run the test individually.
In this image (click the image to see a larger image) you can see the test results for a mixed test
suite. This test suite includes the JUnit 4 test suite and one of the JUnit 3 test classes. The test
suite ran the UtilsJUnit3Test.java and JUnit4TestSuite.java test classes as a single test
and displayed the test results in the left pane as the results of a single test. The output in the right
pane is the same as running the test individually.
Summary
This tutorial was a basic introduction to creating JUnit unit tests and test suites in NetBeans IDE.
The IDE supports JUnit 3 and JUnit 4, and this document demonstrated some of the changes
introduced in JUnit 4 that are designed to make creating and running tests simpler.
As demonstrated in this tutorial, one of the main improvements in JUnit 4 is support for
annotations. In JUnit 4 you can now use annotations to do the following:
For more information about using JUnit and other changes introduced in JUnit 4, see the
following resources:
Testing code often helps ensure that small changes made in the code do not break the
application. Automated testing tools like JUnit streamline the process of testing and frequent
testing can help catch coding errors early.