Simple TDD Case Study. in Java

Similar documents
Annotations in Java (JUnit)

Practical Unit Testing junit. Atul Prakash

What readers are saying about Pragmatic Unit Testing...

Examples. Stack. VideoPlayer

Introduction to JUnit. Data Structures and Algorithms for Language Processing

Object Oriented Software Design - I

Essentials of Embedded Software Engineering

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Tools for Unit Test - JUnit

print statements, debugger expressions, test scripts. Writing expressions in a debugger only that t a program works now. An application typically

4. A Testing Framework. Oscar Nierstrasz

Exceptions and Design

CS159. Nathan Sprague

Tools for Unit Test JUnit

Building Java Programs

Chapter 15. Software Testing The assert Statement

Tuesday, November 15. Testing

COMP 354 TDD and Refactoring

Motivating Example: Two Types of Errors (2) Test-Driven Development (TDD) with JUnit. Motivating Example: Two Types of Errors (1)

Practical Objects: Test Driven Software Development using JUnit

Test-Driven Development (TDD) with JUnit

Lecture 3. Black- box Tes3ng

CS159. Nathan Sprague

Stage 11 Array Practice With. Zip Code Encoding

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (testing whatever occurs to you at

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

CSE 331 Midterm Exam Sample Solution 2/13/12

EECS 4313 Software Engineering Testing

Equivalence Class Partitioning. Equivalence Partitioning. Definition and Example. Example set of classes

Agile Software Development. Lecture 7: Software Testing

Test automation / JUnit. Building automatically repeatable test suites

Test automation Test automation / JUnit

Testing and Debugging

Typed Lambda Calculus and Exception Handling

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Searching & Sorting in Java Bubble Sort

LAB 13: ARRAYS (ONE DIMINSION)

CS18000: Programming I

UNIT TESTING. Krysta Yousoufian CSE 331 Section April 19, With material from Marty Stepp, David Notkin, and The Pragmatic Programmer

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Array Based Lists. Collections

Test-Driven Development JUnit

CSE 331 Final Exam 3/16/15 Sample Solution

16-Dec-10. Consider the following method:

Test automation / JUnit. Building automatically repeatable test suites

Java Review via Test Driven Development

Test-Driven Development JUnit

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

What can go wrong in a Java program while running?

Introduc)on to tes)ng with JUnit. Workshop 3

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

CS159. Nathan Sprague. September 30, 2015

CSCE 747 Unit Testing Laboratory Name(s):

Designing Robust Classes

Testing. CMSC 433 Programming Language Technologies and Paradigms Spring A Real Testing Example. Example (Black Box)?

Software Test. Levels of test. Types of test. Regression test The JUnit tool for unit testing Java programs. System test Integration test Unit test

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

EXCEPTIONS. Prof. Chris Jermaine

White box testing. White-box testing. Types of WBT 24/03/15. Advanced Programming

Testing. Technion Institute of Technology Author: Assaf Israel. Author: Assaf Israel - Technion

Object Oriented Programming. Week 7 Part 1 Exceptions

Unit Testing with JUnit in DrJava *

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 3

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

CS 3 Introduction to Software Engineering. 3: Exceptions

In this lab we will practice creating, throwing and handling exceptions.

Unit testing. JUnit. JUnit and Eclipse. A JUnit test class 3/6/17. A method is flagged as a JUnit test case.

Exceptions. What exceptional things might our programs run in to?

CIS265 - Spring Exam 2 First Name Last Name CSU#

CS-152: Software Testing

Object Oriented Programming Exception Handling

Assertions and Exceptions Lecture 11 Fall 2005

Unit tests. Paulo Cheque. Summer License: Creative Commons: Attribution-Share Alike 3.0 Unported

Lecture 3: Working with Data Structures

Assigned Date: August 27, 2014 Due Date: September 7, 2015, 11:59 PM

CMPSCI 187: Programming With Data Structures. Lecture #21: Array-Based Lists David Mix Barrington 26 October 2012

CSE431 Translation of Computer Languages

Lab #7 Library Classes and JUnit Testing. Daniel Amyot, Diana Inkpen, Alan. Agenda. In this lab, you are going to create your own

Software Testing. Lecturer: Sebastian Coope Ashton Building, Room G.18

Unit testing. unit testing: Looking for errors in a subsystem in isolation. The basic idea: JUnit provides "assert" commands to help us write tests.

Testing Driven Development. Advanced Programming

Problem other classes messing with my stuff!

COMP1008 Exceptions. Runtime Error

CS 163 Practice Final Exam Winter 2012

Software Project Seminar VII: Tools of the Craft. 23 march 2006 Jevgeni Kabanov

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Test First Software Development

Video 2.1. Arvind Bhusnurmath. Property of Penn Engineering, Arvind Bhusnurmath. SD1x-2 1

CSE 331 Midterm Exam 2/13/12

Test-Driven Development (a.k.a. Design to Test) CSE260, Computer Science B: Honors Stony Brook University

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Glossary. (Very) Simple Java Reference (draft, v0.2)

Lists. ordered lists unordered lists indexed lists 9-3

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

Testing Stragegies. Black Box Testing. Test case

Transcription:

Simple TDD Case Study in Java

Assertions To check if code is behaving as you expect, use an assertion, a simple method call that verifies that something is true. E.g the method asserttrue checks that the given boolean condition is true public void asserttrue(boolean condition) if (!condition) abort(); 2

Using Asserts You could use this assert to check all sorts of things, including whether numbers are equal to each other. To check that two integers are equal, a method that takes two integer parameters might be more useful. int a = 2; //... asserttrue (a == 2); public void assertequals (int a, int b) asserttrue(a == b); We can now write the first test a little more expressively: int a = 2; assertequals (2, a); 3

Planning Tests Method to test: A static method designed to find the largest number in a list of numbers. The following tests would seem to make sense: [7, 8, 9] -> 9 public static int largest (int[] list)... [8, 9, 7] -> 9 [9, 7, 8] -> 9 (supplied test data ->expected result) 4

More Test Data + First Implementation Already have this data: [7, 8, 9] -> 9 [8, 9, 7] -> 9 [9, 7, 8] -> 9 What about this set: [7, 9, 8, 9] -> 9 [1] -> 1 [-9, -8, -7] -> -7 public static int largest (int[] list) int index, max = Integer.MAX_VALUE; for (index = 0; index < list.length - 1; index++) if (list[index] > max) max = list[index]; return max; 5

Writing the Test import junit.framework.testcase; This is a TestCase called TestLargest. It has one Unit Test - to verify the behaviour of the largest method. public class TestLargest extends TestCase public TestLargest (String name) super(name); public void testorder () int[] arr = new int[3]; arr[0] = 8; arr[1] = 9; arr[2] = 7; assertequals(9, Largest.largest(arr)); 6

Running the Test Why did it return such a huge number instead of our 9 Where could that very large number have come from? 7

Bug First line should initialize max to zero, not MAX_VALUE. public static int largest (int[] list) //int index, max = Integer.MAX_VALUE; int index, max = 0; for (index = 0; index < list.length - 1; index++) if (list[index] > max) max = list[index]; return max; 8

Further Tests What happens when the largest number appears in different places in the list - first or last, and somewhere in the middle? Bugs most often show up at the edges In this case, edges occur when when the largest number is at the start or end of the array that we pass in Aggregate into a single unit test: public void testorder () assertequals(9, Largest.largest(new int[] 9, 8, 7 )); assertequals(9, Largest.largest(new int[] 8, 9, 7 )); assertequals(9, Largest.largest(new int[] 7, 8, 9 )); 9

Failure + Fix public static int largest (int[] list) int index, max = 0; //for (index = 0; index < list.length - 1; index++) for (index = 0; index < list.length; index++) if (list[index] > max) max = list[index]; return max; 10

Further Boundary Conditions public void testdups () assertequals(9, Largest.largest(new int[] 9, 7, 9, 8 )); public void testone () assertequals(1, Largest.largest(new int[] 1 )); Now exercising multiple tests 11

Failure on testnegative public void testnegative () int[] neglist = new int[] -9, -8, -7 ; assertequals(-7, Largest.largest(negList)); 12

fix testnegative Choosing 0 to initialize max was a bad idea; public static int largest (int[] list) //int index, max = 0; int index, max = Integer.MIN_VALUE; Should have been MIN VALUE, so as to be less than all negative numbers as well for (index = 0; index < list.length; index++) if (list[index] > max) max = list[index]; return max; 13

Expected Errors? public static int largest (int[] list) int index, max = Integer.MIN_VALUE; If the array is empty, this is considered an error, and an exception should be thrown public void testempty () try Largest.largest(new int[] ); fail("should have thrown an exception"); catch (RuntimeException e) asserttrue(true); if (list.length == 0) throw new RuntimeException("Empty list"); for (index = 0; index < list.length; index++) if (list[index] > max) max = list[index]; return max; 14

import junit.framework.testcase; Unit Test Suite Comprehensive suite of tests Offers the freedom to refactor algorithm or even completely replace with alternative version public class TestLargest extends TestCase public TestLargest (String name) super(name); public void testorder () int[] arr = new int[3]; arr[0] = 8; arr[1] = 9; arr[2] = 7; assertequals(9, Largest.largest(arr)); public void testorder () assertequals(9, Largest.largest(new int[] 9, 8, 7 )); assertequals(9, Largest.largest(new int[] 8, 9, 7 )); assertequals(9, Largest.largest(new int[] 7, 8, 9 )); public void testdups () assertequals(9, Largest.largest(new int[] 9, 7, 9, 8 )); public void testone () assertequals(1, Largest.largest(new int[] 1 )); public void testnegative () int[] neglist = new int[] -9, -8, -7 ; assertequals(-7, Largest.largest(negList)); public void testempty () try Largest.largest(new int[] ); fail("should have thrown an exception"); catch (RuntimeException e) asserttrue(true); 15