Baixe Capítulo 44 - Prueba usando JUnit - Liang-Java-Comp11e e outras Resumos em PDF para Informática, somente na Docsity!
Objectives
■ ■ To know what JUnit is and how JUnit works (§44.2).
■ ■ To create and run a JUnit test class from the command window (§44.2).
■ ■ To create and run a JUnit test class from NetBeans (§44.3).
■ ■ To create and run a JUnit test class from Eclipse (§44.4).
Testing Using JUnit
CHAPTER
44-2 Chapter 44 Testing Using JUnit
44.1 Introduction
JUnit is a tool for testing Java programs.
At the very beginning of this book in Section 2.16, we introduced software development pro-
cess that includes requirements specification, analysis, design, implementation, testing, deploy-
ment, and maintenance. Testing is an important part of this process. This chapter introduces
how to test Java classes using JUnit.
44.2 JUnit Basics
To test a class, you need to write a test class and run it through JUnit to generate a
report for the class.
JUnit is the de facto framework for testing Java programs. JUnit is a third-party open-source
library packed in a jar file. The jar file contains a tool called test runner , which is used to run
test programs. Suppose you have a class named A. To test this class, you write a test class
named ATest. This test class, called a test runner , contains the methods you write for testing
class A. The test runner executes ATest to generate a test report, as shown in Figure 44.1.
Point
Key
Point
Key
Figure 44.1 JUnit test runner executes the test class to generate a test report.
Test Runner
Test Class File Test Report … e.g., ATest.class A.class …
Figure 44.2 The JUnit test runner displays the JUnit version.
You will see how JUnit works from an example. To create the example, first you need to
download JUnit from http://sourceforge.net/projects/junit/files/. At present, the latest version
is junit-4.10.jar. Download this file to c:\book\lib and add it to the classpath environment
variable as follows:
set classpath=.;%classpath%;c:\book\lib\junit-4.10.jar
To test if this environment variable is set correctly, open a new command window, and type
the following command:
java org.junit.runner.JUnitCore
You should see the message displayed as shown in Figure 44.2.
To use JUnit, create a test class. By convention, if the class to be tested is named A , the test
class should be named ATest. A simple template of a test class may look like this:
44-4 Chapter 44 Testing Using JUnit
Listing 44.1 is an example of a test class for testing java.util.ArrayList.
Listing 44.1 ArrayListTest.java 1 package mytest; 2 3 import org.junit.; 4 import static org.junit.Assert.; 5 import java.util.*; 6 7 public class ArrayListTest { 8 private ArrayList list = new ArrayList(); 9 10 @Before 11 public void setUp() throws Exception { 12 } 13 14 @Test 15 public void testInsertion() { 16 list.add( "Beijing" ); 17 assertEquals( "Beijing" , list.get( 0 )); 18 list.add( "Shanghai" ); 19 list.add( "Hongkong" ); 20 assertEquals( "Hongkong" , list.get(list.size() – 1)); 21 } 22 23 @Test 24 public void testDeletion() { 25 list.clear(); 26 assertTrue(list.isEmpty()); 27 28 list.add( "A" ); 29 list.add( "B" ); 30 list.add( "C" ); 31 list.remove( "B" ); 32 assertEquals( 2 , list.size()); 33 } 34 }
A test run of the program is shown in Figure 44.3. Note that you have to first compile Array-
ListTest.java. The ArrayListTest class is placed in the mytest package. So you should
place ArrayListTest.java in the directory named mytest.
Figure 44.3 The test report is displayed from running ArrayListTest.
44.2 JUnit Basics 44-
No errors are reported in this JUnit run. If you mistakenly change
assertEquals( 2 , list.size());
in line 32 to
assertEquals( 3 , list.size());
Run ArrayListTest now. You will see an error reported as shown in Figure 44.4.
Figure 44.4 The test report reports an error.
You can define any number of test methods. In this example, the two test methods test-
Insertion and testDeletion are defined. JUnit executes testInsertion and test-
Deletion in this order.
Note The test class must be placed in a named package such as mytest in this example. The JUnit will not work if the test class is placed a default package.
Listing 44.2 gives a test class for testing the Loan class in Listing 10.2. For convenience,
we create Loan.java in the same directory with LoanTest.java. The Loan class is shown in
Listing 44.3.
Listing 44.2 LoanTest.java
1 package mytest; 2 3 import org.junit.; 4 import static org.junit.Assert.; 5 6 public class LoanTest { 7 @Before 8 public void setUp() throws Exception { 9 } 10 11 @Test 12 public void testPaymentMethods() { 13 double annualInterestRate = 2.5 ; 14 int numberOfYears = 5 ; 15 double loanAmount = 1000 ; 16 Loan loan = new Loan(annualInterestRate, numberOfYears, 17 loanAmount); 18
44.2 JUnit Basics 44-
35 /** Return numberOfYears / 36 public int getNumberOfYears() { 37 return numberOfYears; 38 } 39 40 /* Set a new numberOfYears / 41 public void setNumberOfYears( int numberOfYears) { 42 this .numberOfYears = numberOfYears; 43 } 44 45 /* Return loanAmount / 46 public double getLoanAmount() { 47 return loanAmount; 48 } 49 50 /* Set a newloanAmount / 51 public void setLoanAmount( double loanAmount) { 52 this .loanAmount = loanAmount; 53 } 54 55 /* Find monthly payment / 56 public double getMonthlyPayment() { 57 double monthlyInterestRate = annualInterestRate / 1200 ; 58 double monthlyPayment = loanAmount * monthlyInterestRate / ( 1 – 59 ( 1 / Math.pow( 1 + monthlyInterestRate, numberOfYears * 12 ))); 60 return monthlyPayment; 61 } 62 63 /* Find total payment / 64 public double getTotalPayment() { 65 double totalPayment = getMonthlyPayment() * numberOfYears * 12 ; 66 return totalPayment; 67 } 68 69 /* Return loan date */ 70 public java.util.Date getLoanDate() { 71 return loanDate; 72 } 73 }
The testPaymentMethods() in LoanTest creates an instance of Loan (line 16–17) and
tests whether loan.getMonthlyPayment() returns the same value as getMonthlyPayment
(annualInterestRate, numberOfYears, loanAmount). The latter method is defined
in the LoanTest class (lines 28–34).
Figure 44.5 The JUnit test runner executes LoanTest and reports no errors.
44-8 Chapter 44 Testing Using JUnit
The testPaymentMethods() also tests whether loan.getTotalPayment() returns
the same value as getTotalPayment(annualInterestRate, numberOfYears, loan-
Amount). The latter method is defined in the LoanTest class (lines 37–41).
A sample run of the program is shown in Figure 44.5.
44.2.1 What is JUnit? 44.2.2 What is a JUnit test runner? 44.2.3 What is a test class? How do you create a test class? 44.2.4 How do you use the assertTrue method? 44.2.5 How do you use the assertEquals method?
44.3 Using JUnit from NetBeans
JUnit is intergrated with NetBeans. Using NetBeans, the test program can be auto-
matically generated and the test process can be automated.
An IDE such as NetBeans and Eclipse can greatly simplify the process for creating and run-
ning test classes. This section introduces using JUnit from NetBeans, and the next section will
introduce using JUnit from Eclipse.
If you are not familiar with NetBeans, see Supplement II.B. Assume you have installed
NetBeans 8 or higher. Create a project named chapter44 as follows:
Step 1: Choose File , New Project to display the New Project dialog box.
Step 2: Choose Java in the Categories section and Java Application in the Projects
section. Click Next to display the New Java Application dialog box.
Step 3: Enter chapter44 as the Project Name and c:\book as Project Location. Click
Finish to create the project as shown in Figure 44.6.
To demonstrate how to create a test class, we first create a class to be tested. Let the class be
Loan from Listing 10.2. Here are the steps to create the Loan class under chapter.
Point
Check
Point
Key
Figure 44.6 A new project named chapter44 is created.
44-10 Chapter 44 Testing Using JUnit
Figure 44.9 You should select JUnit 4.x framework to create test classes.
Figure 44.10 The LoanTest class is automatically generated.
Step 2: Click OK. You will see the Select JUnit version dialog box displayed as
shown in Figure 44.9. Choose Junit 4.x. Click OK to generate a test class named
LoanTest as shown in Figure 44.10. Note that LoanTest.java is placed under the Test
Packages node in the project.
44.4 Using JUnit from Eclipse 44-
44.4 Using JUnit from Eclipse
JUnit is intergrated with Eclipse. Using Eclipse, the test program can be automati-
cally generated and the test process can be automated.
This section introduces using JUnit from Eclipse. If you are not familiar with Eclipse, see
Supplement II.D. Assume you have installed Eclipse 4.5 or higher. Create a project named
chapter50 as follows:
Step 1: Choose File , New Java Project to display the New Java Project dialog box, as
shown in Figure 44.12.
Step 2: Enter chapter50 in the project name field and click Finish to create the
project.
To demonstrate how to create a test class, we first create a class to be tested. Let the class be
Loan from Listing 10.2. Here are the steps to create the Loan class under chapter.
Point
Key
Figure 44.11 The test report is displayed after the LoanTest class is executed.
You can now modify LoanTest by copying the code from Listing 44.2. Run LoanTest.java.
You will see the test report as shown in Figure 44.11.
44.4 Using JUnit from Eclipse 44-
Figure 44.13 The New Java Class dialog creates a new Java class.
Figure 44.14 The Loan class is created.
44-14 Chapter 44 Testing Using JUnit
Figure 44.15 The New JUnit Test Case dialog box creates a Test class.
Figure 44.16 The LoanTest class is automatically generated.