Test patterns.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Written by: Dr. JJ Shepherd
March 2004Object Oriented Design1 Object-Oriented Design.
Xpath Sources:
Programmer Testing Testing all things Java using JUnit and extensions.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
1 Web Based Programming Section 6 James King 12 August 2003.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
CPSC 872 John D. McGregor Session 21 Architecture Design, cont’d.
Partitioning Patterns How to partition complex actors and concepts into multiple classes. Layered Initialization Filter Composite.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Unit Testing Part 2: Drivers and Stubs
The Singleton Pattern (Creational)
Today’s lecture Review of chapter 8 Go over examples.
Test Code Patterns How to design your test code. 2 Testing and Inheritance Should you retest inherited methods? Can you reuse superclass tests for inherited.
Multi-purpose tests (Cool tricks with JUnit) JavaZone 2012 Johannes Brodwall, Principal Architect Steria
Lecture IX: Testing Web Services with Mocking CS 4593 Cloud-Oriented Big Data and Software Engineering.
Test Isolation and Mocking Technion – Institute of Technology Author: Gal Lalouche © 1 Author: Gal Lalouche - Technion 2016 ©
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Exceptions throw new RuntimeException(“bad things happened”); The above line is the simplest possible way of throwing an exception. In Java, exceptions.
Lecture 8 D&D Chapter 9 Inheritance Date.
Topics: Templates Exceptions
File handling and Scanning COMP T1
Test Isolation and Mocking
Tirgul 13 Exceptions 1.
Introduction to JUnit CS 4501 / 6501 Software Testing
Designing For Testability
Exceptions, Interfaces & Generics
Coding Defensively Coding Defensively
Introduction to Exceptions in Java
University of Central Florida COP 3330 Object Oriented Programming
Accessing Files in Java
Handling Exceptions.
EE422C Software Implementation II
Software Construction
Exception Handling Chapter 9.
Mocking Your Objects with Impunity
null, true, and false are also reserved.
Introduction to Java Programming
Exceptions Complicate Code
Exceptions Complicate Code
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
Exception Handling Chapter 9 Edited by JJ.
DEFENSIVE PROGRAMMING
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
More JUnit CS 4501 / 6501 Software Testing
CSE 143 Java Exceptions 1/18/2019.
Exceptions 1 CMSC 202.
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Exceptions Complicate Code
Go to pollev.com/cse143.
CMSC 202 Exceptions 2nd Lecture.
Ninth step for Learning C++ Programming
Tenth step for Learning C++ Programming
Chapter 2. Problem Solving and Software Engineering
Computer Science 340 Software Design & Testing
Exceptions one last time…
Test-Driven Development
CMSC 202 Lesson 20 Exceptions 1.
Software Construction
Exceptions and Exception Handling
Plug-In Architecture Pattern
Presentation transcript:

Test patterns

Test patterns There are several situations testers face often for which established solutions exist We will discuss three of these test patterns Crash Test Dummy Log String Mock objects

Crash Test Dummy Most software systems contain a large amount of error handling code Sometimes, it is quite hard to create the situation that will cause the error Example: Error creating a file because the file system is full Solution: Fake it!

import java.io.File; import java.io.IOException; class FullFile extends File { public FullFile(String path) { super(path); } public boolean createNewFile() throws IOException { throw new IOException();

public void testFileSystemFull() { File f = new FullFile("foo"); try { saveAs(f); fail(); } catch (IOException e) {}

public void testFileSystemFull() { File f = new FullFile("foo") { public boolean createNewFile() throws IOException { throw new IOException(); } }; try { saveAs(f); fail(); catch (IOException e) {}

Log String Often one needs to test that the sequence in which methods are called is correct Solution: Have each method append to a log string when it is called Then, assert that the log string is the correct one Requires changes to the implementation

Mock Objects How do you test an expensive or complicated resource? Databases take a long time to start Solution: Have a mock object that acts like a database Database db = new MockDatabase(); db.query("select ...."); db.result("Fake Result");

Mock objects If the MockDatabase does not get the query it expects, it throws an exception If the query is correct, it returns the provided result Caveat: the mock object needs to behave exactly like the real one Need to have tests for the mock object that are run less frequently

Accessing private fields Object-oriented design guidelines often designate that certain fields should be private / protected This can be a problem for testing since a tester may need to assert certain conditions about private fields Making these fields public defeats the purpose

A solution Using reflection, one can actually call private methods and access private attributes! An example class A { private String sayHello(String name) { return "Hello, " + name; }

import java.lang.reflect.Method; public void testPrivateMethod { A test = new A(); Method[] methods = test.getClass().getDeclaredMethods(); for (int i = 0; i < methods.length; ++i) { if (methods[i].getName().equals("sayHello")) { Object params[] = {"Ross"}; methods[i].setAccessible(true); Object ret = methods[i].invoke(test, params); System.out.println(ret); }