Professional Documents
Culture Documents
Programming 1A: (PROG5121)
Programming 1A: (PROG5121)
Programming 1A: (PROG5121)
(PROG5121)
WRITING JUNIT TESTS IN
NETBEANS IDE
• It’s usually the responsibility of one or more software developers to do the unit testing, typically
during the development phase of a software product.
• The purpose of doing unit testing is to validate (test the correctness of) of a unit component.
• JUnit is a unit testing framework for the Java programming language (one amongst others like
Serenity and Selenium).
• JUnit has been important in the development of test-driven development, and is one of a family
of unit testing frameworks collectively known as xUnit, that originated with JUnit.
• For the purposes of this module and the fact that JUnit is widely used in practice, the unit testing
example that follows is based on Junit.
CREATING A NEW PROJECT TO TEST (1)
1. Enter ‘Messages’
as project name.
2. Enter the path to
you where your
project is located
on your computer.
3. Enter your ‘Group
Id’ that identifies
your organization.
4. Click ‘Finish’.
CREATING A CLASS TO TEST
*/
package za.co.vc.messages;
/**
1. Write the usual
* @author John Daratos comments at the top
of the class file and
* @version JDK 1.13 Javadoc for the class.
2. Create a method for
* @since JDK 1.8 the class called
writeMessage().
*/
}
CREATING A TEST FOR THE CLASS
@Test
}
CUSTOMIZING THE GENERATED TEST CODE
@Test
• Replace the generated
public void testHelloWorld() {
code in
System.out.println("Testing the testHelloWorld() method."); testHelloWorld() with
customized code.
Message instance = new Message(); • Test the code by right-
clicking on the code
String result = instance.helloWorld(); editor, and then clicking
on 'Test File' (or
assertEquals(result, "Hello World"); pressing the keys
Ctrl+F6).
}
TEST REPORTS
The Surefire Plugin is used during the test phase of the build lifecycle
to execute the unit tests of an application. It generates reports in two
Results are also reported via the Test
different file formats: Results window.
1. Plain text files (*.txt)
2. XML files (*.xml)
CLASS ACTIVITY 6
return a + b;
return a * b;
Now follow the steps you learned in this lecture to create a test for each one:
1. For sum(), the test must fail if the sum is not equal to 5 when a = 2 and b = 3.
2. For multiplication(), the test must fail if it’s not equal to 15 when a = 5 and b = 3.