Unit Testing CSIS 3701: Advanced Object Oriented Programming.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Methods
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Testing Testing Techniques to Design Tests. Testing:Example Problem: Find a mode and its frequency given an ordered list (array) of with one or more integer.
Exception Handling Part 2: Creating and Throwing Exceptions CSIS 3701: Advanced Object Oriented Programming.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Unit Testing Part 2: Drivers and Stubs
Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Classes - Intermediate
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Take-Home Lab #02 CS1020 – DATA STRUCTURES AND ALGORITHMS 1 AY SEMESTER 2 1.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
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.
Java Course Review.
CSIS 3701: Advanced Object Oriented Programming
Java Programming: Guided Learning with Early Objects
Inheritance and Encapsulation
Class Definitions and Writing Methods
Chapter Three - Implementing Classes
Writing Methods.
Phil Tayco Slide version 1.0 Created Oct 9, 2017
slides created by Ethan Apter
slides created by Ethan Apter
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Building Java Programs
Week 4 Lecture-2 Chapter 6 (Methods).
Introduction to Java Brief history of Java Sample Java Program
Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”
slides created by Ethan Apter and Marty Stepp
Computer Science Club 1st November 2019.
CS100A Lect. 12, 8 Oct More About Arrays
Presentation transcript:

Unit Testing CSIS 3701: Advanced Object Oriented Programming

Verification and Validation Does support class meet all requirements? Validation –Are there methods and constructors to meet all needs of client programmers? –Done by thorough class design, integration testing Verification –Do individual methods/constructors work correctly? –Done with unit testing

Methods vs. Functions Function correctness: –Produces desired output for all legal inputs Method correctness: –Produces desired output and desired next state for all legal inputs and legal current states function inputdesired output method input desired output current statedesired next state

Viewing State Method correctness based on resulting object state as well as return value Must be able to view state in order to verify –c.setHour(15); Is hour now 15? –c.setHour(37); Is hour not 37?

Viewing State Assumption: Have working toString() method –Returns string showing all member variables –Can print to standard output for debugging: System.out.println(object.toString()); –Can call inside methods (like scaffolding): public boolean add(String name) { … System.out.println(toString()); }

Test Case Design Test cases based on requirements –Created during design/analysis –Can be refined to cover all possible outcomes Legal time Illegal time Change time Illegal hour Illegal minute hour < 0 hour > 23 minute < 0 minute > 59

Test Case Design Often involves boundary testing –Similar cases with different outcomes –Data structures nearly empty/full Clock class: setHour(int h)h : …-1 | 0 … 23 | 24 NameList class: add(String name) current : …0 | 1 … maximum - 1 | maximum

Constructors Must put object in correct initial state –Based on any parameters –Must test each overloaded constructor Examples: – Clock()  test hour == 0, minute == 0 – Clock(13,37)  test hour == 13, minute == 37 – NameList(5)  test maximum == 5 current == 0 no names in list

Modifiers Must put object in correct state –At least one test for each way state can be changed nextMinute() in Clock class –Simple minute increment  time = 11:23 –Increment hour  time = 11:59 –Reset hour to 0  time = 23:59 add() in NameList class –Add new name to empty list –Add new name to non-empty list –Add new name to list almost full Is new name in list? Are existing names unchanged? Was current incremented?

Modifiers Must do proper validation –At least one test for each way state can be invalid –Boundary testing –Make sure state unchanged add() in NameList class –Add existing name to list Existing name at beginning, middle, end –Add new name to full list Is new name not in list? Are existing names unchanged? Is current unchanged?

Inspectors Must return correct information for current state –Correct format –No change to object state –Validation of input (if relevant) getNamesAsArray() –Test on list  returns correct array? –Test on empty list  no errors? getNamesByIndex(int) –Test on values 0 to current - 1 –Boundary tests -1, current should return null No change to names, current, or maximum

Inspectors Validation inspectors –At least one test for each way state can be invalid –Boundary testing isFull() –Test on empty list,list with one space left –Test on full list isIn(String) –Test on all names in list –Test on name not in list

Planning Test Cases Often need to “set up” tests with method calls –Tests often based on specific state –Use modifiers to put object in necessary state isIn(“Fred”) –Must use add(“Fred”) first to place in list isFull() –Must use add() first to fill up list

Planning Test Cases Can “script” test cases: NameList n = new NameList(3); n.add(“a”); n.add(“b”); n.add(“c”); // test for isFull() System.out.println(toString());

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 Main.java Stores names Returns current names Validates list not full, name not already in list NameList.java names Are errors here? Or are they in the testing tool itself? Current application may not use all methods in class

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

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 Constructors and methods main method to test these NameList.java

Class Drivers in Java Example: Simple driver for add method in NameList public static void main(String[] args) { Scanner s = new Scanner(System.in); NameList test = new NameList(3); while(true) { System.out.print("Enter name: "); String name = s.nextLine(); test.add(name); System.out.println(test.toString()); } }

Planning Test Cases Order of tests important –If method depends on other methods/constructor, must verify them first Examples: –Must verify constructor before testing methods –Must verify ability to view state before testing methods –Must verify ability to change state before testing inspectors based on state –Must verify validation inspectors before testing methods that use them for validation

Planning Test Cases Verify Constructor NameList() Verify inspector toString() Must be able to view state before testing methods Example: NameList class Verify add(String) in cases with no error Must be able to add names before testing ability to view names Verify getNamesAsArray(), getNamesByIndex() inspectors Verify isIn(String), isFull() inspectors Verify add(String) in cases with full list, name already in list