Mock objects.

Slides:



Advertisements
Similar presentations
Test process essentials Riitta Viitamäki,
Advertisements

Integration testing Satish Mishra
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Modeling System Events Adapted from: Systems Analysis and Design in a Changing World, 2nd Edition by John W. Satzinger, Robert Jackson and Stephen Burd.
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
1 CMSC 132: Object-Oriented Programming II Software Development III Department of Computer Science University of Maryland, College Park.
Unit Testing Tips and Tricks: Database Interaction Louis Thomas.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Mock Objects. What are Mock Objects  Any dummy object that stands in for a real object that is not available, or is difficult to use in a test case 
Programmer Testing Testing all things Java using JUnit and extensions.
Basic Concepts The Unified Modeling Language (UML) SYSC System Analysis and Design.
Struts 2.0 an Overview ( )
1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.
Programming Languages and Paradigms Object-Oriented Programming.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Managing the development and purchase of information systems (Part 1)
CSC 213 – Large Scale Programming. Why Do We Test?
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Chapter 8 – Software Testing Lecture 1 1Chapter 8 Software testing The bearing of a child takes nine months, no matter how many women are assigned. Many.
Capture-Replay Mocks Scandinavian Developer Conference 4 th April 2011 Geoff Bache.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Programming & Scratch. Programming Learning to program is ultimately about learning to think logically and to approach problems methodically. The building.
INT-Evry (Masters IT– Soft Eng)IntegrationTesting.1 (OO) Integration Testing What: Integration testing is a phase of software testing in which.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
2010 Control System Writing and Reading the cRio Presented By: Frank Larkin Lansdale Catholic Robotics, Team 272 First 2010 Kickoff, Jan
@DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise
1 Some initial Design suggestions… Getting started… where to begin? Find out whether your design architecture will work… as soon as possible. If you need.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
Test Isolation and Mocking Technion – Institute of Technology Author: Gal Lalouche © 1 Author: Gal Lalouche - Technion 2015 ©
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
(c) University of Washington01-1 CSC 143 Java Programming as Modeling Reading: Ch. 1-6.
Mock Objects in Action Paulo Caroli & Sudhindra Rao Agile 2009 © ThoughtWorks 2008.
Using Mock Objects with Test Driven Development Justin Kohlhepp
Refactoring & Testability. Testing in OOP programming No life in flexible methodologies and for refactoring- infected developers without SOME kind of.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Object-Oriented Principles Applications to Programming.
Advanced Object-Oriented Design Patterns and Architectures Part One COEN396A John Xiao
Software Engineering1  Verification: The software should conform to its specification  Validation: The software should do what the user really requires.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Winter 2007SEG2101 Chapter 121 Chapter 12 Verification and Validation.
Design and implementation Chapter 7 – Lecture 1. Design and implementation Software design and implementation is the stage in the software engineering.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Agenda  Quick Review  Finish Introduction  Java Threads.
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 ©
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Testing Tutorial 7.
Programming & Scratch.
Types for Programs and Proofs
Test Isolation and Mocking
Introduction to JUnit CS 4501 / 6501 Software Testing
Designing For Testability
Putting Testing First CS 4501 / 6501 Software Testing
CSE 374 Programming Concepts & Tools
Design by Contract Fall 2016 Version.
Lesson 2: Building Blocks of Programming
Mocking Your Objects with Impunity
Introduction to Data Structures
Testing and Test-Driven Development CSC 4700 Software Engineering
Introduction to JUnit CS 4501 / 6501 Software Testing
Chapter 14 Abstract Classes and Interfaces
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
Designing For Testability
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Test-Driven Development
Lecture 34: Testing II April 24, 2017 Selenium testing script 7/7/2019
Presentation transcript:

Mock objects

Unit tests The objective of unit testing: to exercise ONE method at a time. Unit tests v.s. Integration tests Unit tests cover code in a class – without touching real dependencies. Integration tests touch concrete dependencies. Unit tests are fast, Integration tests do not need to be. Unit tests do not touch databases, web services, etc. Integration tests do.

Why mocks? What if that method depends on other things? a network? a database? a servlet engine? Other parts of the system? We don’t want to initialize lots of components just to get the right context for one test to run. Solution: Using Mocks / Test doubles

Example Original: Stub out the system call: Something more object-oriented? public long getTime() { return System.getCurrentTimeMillis(); } public long getTime() { if (debug) { return debugCurrentTime; } else { return System.getCurrentTimeMillis(); }

When we need mocks? The real object has non-deterministic behavior (smt random, unpredictable) The real object is difficult to set up The real object has behavior that is hard to trigger (a network error…) The real object is slow The real object has (or is) a user interface The test needs to ask the real object about how it was used (e.g. check to see that log message is actually logged by the object under test) The real object does not exist (a common problem when interfacing with other teams or new hardware systems).

Steps to use mocks for testing Use an interface to describe the object Implement the interface for production code Implement the interface in a mock object for testing Note: the code under test only ever refers to the object by its interface, so it have no idea whether it is using the real object or the mock object.

Example (cont.): real and mock objects public interface Environmental { public long getTime(); // other methods omitted… } public class SystemEnvironment implements Environmental { public long getTime() { return System.getCurrentTimeMillis(); } // other methods public class MockSystemEnvironment implements Environmental { private long currentTime; public void setTime(long aTime) { currentTime = aTime; } public long getTime() { return currentTime; } // other methods

Example (cont.): use mock in testing public Timer { private Environmental env; Timer(Environmental env) { this.env = env; …} public void reminder() { long t = env.getTime(); if (laterThan5pm(t)) env.ringBell(“ringing.wav”); } Example (cont.): use mock in testing public TestClassUnderTest { @Test public void testACaseOfMethodUnderTest() { MockSystemEnvironment env = new MockSystemEnvironment(); Calendar cal = Calendar.getInstance(); cal.set….. // to the 16:59:00, December 31, 2012 long t = cal.getTimeMillis() env.setTime(t); Timer timer = new Timer(env) ; timer.reminder(); assertFalse(env.bellWasRung()); //too early to ring t += (5*60*100); // advance the timer 5 minutes env.resetBell(); assertTrue(env.bellWasRung()); }

Example (cont.): real and mock objects public class MockSystemEnvironment implements Environmental { private long currentTime; public void setTime(long aTime) { currentTime = aTime; } public long getTime() { return currentTime; } private boolean bellRung = false; public void ringBell(String s) { bellRung = true;} public void resetBell() { bellRung = false;} public boolean wasBellRung() { return bellRung;} // other methods

Mock framework Too much trouble to write mocks? There are frameworks available. Java: JMock, EasyMock Objective C: OCMock?

Testing models Interaction Based Testing - you specify certain sequence of interactions between objects, initiate an action, and then verify that the sequence of interactions happened as you specified it. State Based Testing - you initiate an action, and then check for the expected results (return value, property, created object, etc). Record & Replay model - a model that allows for recording actions on a mock object, and then replaying and verifying them. All mocking frameworks uses this model. implicitly (NMock, TypeMock.Net, NMock2) explicitly (EasyMock.Net, Rhino Mocks).

Without and with mocks/test doubles

References http://www.michaelminella.com/testing/unit-testing-with-junit-and-easymock.html http://www.easymock.org/EasyMock3_0_Documentation.html http://jmock.org/getting-started.html