Unit Testing Part 2: Drivers and Stubs

Slides:



Advertisements
Similar presentations
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Advertisements

Written by: Dr. JJ Shepherd
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Computer Science 1620 Programming & Problem Solving.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
1 4.3 Example: Volume of a Sphere Given the radius r, what is the weight of a ball (sphere) of wound twine?Given the radius r, what is the weight of a.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
1 Web Based Programming Section 6 James King 12 August 2003.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Lesson 7: Improving the User Interface
1 Chapter 9 Scope, Lifetime, and More on Functions.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSC 204 Programming I Loop I The while statement.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Programming Language
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CS1101X: Programming Methodology Recitation 3 Control Structures.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld.
Exception Handling Part 2: Creating and Throwing Exceptions CSIS 3701: Advanced Object Oriented Programming.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
1 Chapter 9 Scope, Lifetime, and More on Functions.
Written by: Dr. JJ Shepherd
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Exception Handling How to handle the runtime errors.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Information and Computer Sciences University of Hawaii, Manoa
CSIS 3701: Advanced Object Oriented Programming
Class Structure 15-Jun-18.
Inheritance and Encapsulation
Building Java Programs
Programming in Java Assertion.
Web Design & Development Lecture 6
LCC 6310 Computation as an Expressive Medium
Presentation transcript:

Unit Testing Part 2: Drivers and Stubs CSIS 3701: Advanced Object Oriented Programming

Testing Support Classes Support class not executable program Need another executable program to run tests Construct support objects Call methods Display/check object state for correctness Key: other “tester” program must be simple Any errors must be in support class, not tester!

NameList Example NameList.java Main.java Are errors here? Current application may not use all methods in class NameList.java Main.java Stores names Returns current names Validates list not full, name not already in list names Are errors here? Or are they in the testing tool itself?

Drivers Simple program to test other code Example: function driver for C++ sqrt function int main() { double test; while(1) { cout << “Enter test case: “; cin >> test; cout << sqrt(test) << ‘\n’; } }

main method to test these Class Drivers in Java Must be in a main method Separate testing class As main in support class itself Idea: keep testing tools as part of class Do not delete when finished testing Will need again if class modified in future NameList.java Constructors and methods main method to test these

Class Drivers in Java public static void main(String[] args) { Example: Simple driver for add method in NameList public static void main(String[] args) { NameList n = new NameList(3); while(true) { String name = JOptionPane.showInputDialog(null, "Enter name"); n.add(name); JOptionPane.showMessageDialog(null, n.toString()); }

Class Drivers in Java public static void main(String[] args) { May need additional code for exceptions public static void main(String[] args) { NameList n = new NameList(3); while(true) { String name = JOptionPane.showInputDialog(null, “Name:"); try {n.add(name);} catch (FullListException ex) { JOptionPane.showMessageDialog(null, "List full!"); } catch (InListException ex) { JOptionPane.showMessageDialog(null, “In list!"); JOptionPane.showMessageDialog(null, n.toString());

Class Dependency What if your class depends on other classes? May not be available immediately if being developed by others Still need to compile and test your code Cannot afford to wait around for others! Example: Developing main visual application without working NameList class

Class Dependency Main.java NameList.java This class does not exist yet NameList constructor void add(String) int getSize() String getNameByIndex(int) boolean isIn(String) boolean isFull() NameList names This class does not exist yet Nor do these constructors or methods

NameList Example None of the following code will work! else { public void actionPerformed(ActionEvent e) { String name = nameField.getText(); if (names.isIn(name)) JOptionPane.showMessageDialog(this, name + " already in list"); else { names.add(name); for (int i = 0; i < names.getSize(); i++) namesArea.append(names.getNameByIndex(i) + "\n"); if (names.isFull()) addButton.setEnabled(false); }

Stubs Stub: temporary code to take place of code still in development Replaced with actual code when available Must allow thorough testing of your code All methods, branches, etc. Must be simple Easy to create Unlikely to contain own bugs

Stub Function Example Simple C++ example: void function() {} Your code: … function(); … stub code: void function() {} Function written by other developer and not currently available Simplest possible stub – will allow your code to compile and run

Stub Function Example Can include diagnostic messages to help test Is external function called at correct time? Is it called with correct parameters? Your code: … function(a); … stub code: void function(int x) { cout << “function called with “ << x << “ as parameter\n”; } Diagnostic message displayed

Stub Function Example May need to return value to help test your code Can be simple “hard wired” value Can prompt for return value if needed to test branches int function(int x) { cout << “function called with “ << x << “ as parameter\n”; cout << “What should it return?”; int y; cin >> y; return y; } b = function(a); if (b > 0) { … } else {…} function must return values greater than and less than 0 to test both branches Can perform one test that returns 1 and another that returns -1

Stub Classes Takes place of entire class during testing No state variables Stubs for constructors/methods called by your code Example: NameList stub for testing main: Stub NameList constructor  display parameter Stub add method  display parameter, prompt for whether to throw exception Stub getSize method  prompt for int to return Stub isIn method  prompt to return true or false Stub isFull method  prompt to return true or false Stub getNameByIndex method  return dummy value

NameList Stub public class NameListStub { public NameListStub(int max) { System.out.println("constructor called with "+max); } public String getNameByIndex(int index) { return "Stub Name"; public int getSize() { String response = JOptionPane.showInputDialog(null, "What is list size?"); return Integer.parseInt(response);

NameList Stub public boolean isIn(String name) { String response = JOptionPane.showInputDialog(null, "Is "+name+" in list?"); return response.equals("true"); } public boolean isFull() { String response = JOptionPane.showInputDialog(null, "Is list full?");

NameList Stub public void add(String name) { System.out.println("add called with "+name); String response = JOptionPane.showInputDialog(null, “1) No exception\n2 List full\n3) In list"); if (response.equals(“1”)) return; if (response.equals(“2”)) throw new FullListException(); if (response.equals(“3”)) throw new InListException(); } Prompt for whether to throw an exception Throw appropriate exception type (must have stubs for these also)!