Programming 1A: (PROG5121)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

PROGRAMMING 1A

(PROG5121)
WRITING JUNIT TESTS IN
NETBEANS IDE

BASED ON APACHE NETBEANS 13 DOCUMENTATION.


THE SOFTWARE TESTING HIERARCHY
Determines whether the
software product meets
the required
specifications.

Validates the complete and fully


integrated software product.

Combines and tests individual


software components as a group.

Tests each unit (individual


component) of the software
application.
UNIT TESTING OVERVIEW
• Unit testing is the first level of functional testing that validates a software application against
its functional requirements/specifications.

• 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.

• A unit component is an individual function or procedure that’s expected to produce a


predefined, measurable result.

• 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. Select ‘File’, then


‘New Project’ to see
this window.
2. Select the category
‘Java with Maven’.
3. Select ‘Java
Application’.
4. Click ‘Finish’.
CREATING A NEW PROJECT TO TEST (2)

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

1. Select the package


name, then right-
click on it.
2. Select ‘New’, then
‘Java Class…’ to see
this window.
3. Enter the class
name as ‘Message’.
4. Click ‘Finish’.
CREATING A SIMPLE METHOD TO TEST
/*

* This is a class with a simple method 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().
*/

public class Message {

public String helloWorld() {

return "Hello World";

}
CREATING A TEST FOR THE CLASS

1. Select the class file


name (Message.java).
2. Click on ‘Tools’.
3. Select ‘Create/Update
Tests’ to see this
window.
4. Click ‘OK’.
REVIEWING THE GENERATED TEST CODE

@Test

public void testHelloWorld() {

System.out.println("helloWorld"); • This is not the complete


code that’s generated
Message instance = new Message(); by NetBeans.
• For now, the @Test
String expResult = ""; annotated method
testHelloWorld() will
String result = instance.helloWorld();
do.
assertEquals(expResult, result); • The other generated
methods are optional,
// TODO review the generated test code and remove the default call to fail. executed under specific
conditions.
fail("The test case is a prototype.");

}
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

Create two more methods in the Message class as follows:

public int sum(int a, int b) {

return a + b;

public int multiplication(int a, int 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.

You might also like