Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 42 Testing Using JUnit.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Java Testing Tools. junit is a testing harness for unit testing. emma is a code coverage tool. The tools can be used in concert to provide statement and.
J-Unit Framework.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Multithreading Example from Liang textbook.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Unit Testing. Topics Motivation JUnit framework Basic JUnit tests – static methods Ensure exceptions – instance methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 20 Recursion.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
Unit Testing Testing. Plan Java jUnit (test cases) Emma (coverage) C Check (test cases) Splint (static checking)
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 1 Introduction to Computers, Programs,
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 35 JavaServer Page.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Principles of Object Oriented Programming Practical session 2 – part A.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2 Additional Features of Java Programming.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
JUnit Dwight Deugo Nesa Matic
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Software Design– Unit Testing SIMPLE PRIMER ON Junit Junit is a free simple library that is added to Eclipse to all automated unit tests. The first step,
Netbeans QuickStart. Creating a project File->New Project –For now you want General->Java Application –Then fill in the project details.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
This presentation is created for the course COP4331 at UCF
COMP 103 Testing with JUnit 2016-T2 extras
Installing and running the local check projects in Eclipse
Data types, Expressions and assignment, Input from User
Junit with.
JUnit Automated Software Testing Framework
How to Run a Java Program
Introduction to Software Testing Chapter 3 Test Automation
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Introduction to Computer Science and Object-Oriented Programming
Chapter 3 Numerical Data
Unit Testing with JUnit
Introduction to JUnit IT323 – Software Engineering II
Testing Acknowledgement: Original slides by Jory Denny.
Chapter 50 Testing Using JUnit
CSE 143 Lecture 5 More ArrayIntList:
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Workshop for Programming And Systems Management Teachers
JUnit Tutorial Hong Qing Yu Nov 2005.
Principles of Object Oriented Programming
Presentation transcript:

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 42 Testing Using JUnit

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2 Objectives F To know what JUnit is and how JUnit works (§42.2). F To create and run a JUnit test class from the command window (§42.2). F To create and run a JUnit test class from NetBeans (§42.3). F To create and run a JUnit test class from Eclipse (§42.4).

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 JUnit Basics 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 class, contains the methods you write for testing class A. The test runner executes ATest to generate a test report, as shown in Figure 42.1.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4 Obtaining and Running JUnit 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 will see how JUnit works from an example. To create the example, first you need to download JUnit from 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

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5 A JUnit Test Class 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: package mytest; import org.junit.*; import static org.junit.Assert.*; public class ATest public void m1() { // Write a test method public void m2() { // Write another test method public void setUp() throws Exception { // Common objects used by test methods may be set up here }

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6 Run the Test java org.junit.runner.JUnitCore mytest.ATest To run the test from the console, use the following command:

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7 Test ArrayList Listing 42.1 is an example of a test class for testing java.util.ArrayList. package mytest; import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class ArrayListTest { private ArrayList list = new ArrayList public void setUp() throws Exception { public void testInsertion() { list.add("Beijing"); assertEquals("Beijing", list.get(0)); list.add("Shanghai"); list.add("Hongkong"); assertEquals("Hongkong", list.get(list.size() - 1)); public void testDeletion() { list.clear(); assertTrue(list.isEmpty()); list.add("A"); list.add("B"); list.add("C"); list.remove("B"); assertEquals(2, list.size()); }

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8 Test the Loan Class package mytest; import org.junit.*; import static org.junit.Assert.*; public class LoanTest public void setUp() throws Exception { public void testPaymentMethods() { double annualInterestRate = 2.5; int numberOfYears = 5; double loanAmount = 1000; Loan loan = new Loan(annualInterestRate, numberOfYears, loanAmount); assertTrue(loan.getMonthlyPayment() == getMonthlyPayment(annualInterestRate, numberOfYears, loanAmount)); assertTrue(loan.getTotalPayment() == getTotalPayment(annualInterestRate, numberOfYears, loanAmount)); } /** Find monthly payment */ private double getMonthlyPayment(double annualInterestRate, int numberOfYears, double loanAmount) { double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12))); return monthlyPayment; } /** Find total payment */ public double getTotalPayment(double annualInterestRate, int numberOfYears, double loanAmount) { return getMonthlyPayment(annualInterestRate, numberOfYears, loanAmount) * numberOfYears * 12; }