CS173: Intro to Computer Science - Unit Testing with the NetBeans Software Environment
This guide has been adapted from Professor Tralie.
Unit Testing
Let’s say we want to test the code method we provided in the introduction article with JUnit. You can follow the instructions in that article to create the sample project in NetBeans. Then, right click on the class file, then choose Tools -> Create/Update tests
on the primary class of that project.
Then, check/uncheck the following boxes
This will create a new file for testing with an example test method for each method you created in your original class. You should be careful to comment out or delete the fail
line at the end of the test, and be sure that your inputs and expected outputs are the expected behavior of your method. Once you’ve done this, you can right click on this file and click run
, and the results of the test will be shown in a window:
If you are interested in trying this test case out for yourself, here is the code from the screenshot above. This test case asks if 1
is an even number, and should pass because it expects that the function will return false
.
import org.junit.*;
import static org.junit.Assert.*;
public class NewClassTest {
public NewClassTest() {
}
@Test
public void testIsEven() {
System.out.println("isEven");
int num = 1;
boolean expResult = false;
boolean result = NewClass.isEven(num);
assertEquals(result, expResult);
//fail("The test case failed");
}
}
You will notice that the junit
library comes with some new functions: assertEquals
and fail
. assertEquals
will check that the two parameters (for example, result
and expResult
above) are equal, and fail the test if they are not. There are other assert
methods as well, that check for other conditions. The fail
method automatically fails the test, and is suitable for use in your own if
statements. If your test function executes to the end without failing somewhere (either by failing an assertion or by calling fail
directly), the test is considered to have passed.