Integration Testing When testing a module in isolation –Called modules need to be replaced –Tested module needs to be called A D ′ E ′ main driver module.

Slides:



Advertisements
Similar presentations
Testing Relational Database
Advertisements

Testing Object Oriented Programs CSE 111 4/28/20151.
Remote Procedure Call (RPC)
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
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.
Programmer Testing Testing all things Java using JUnit and extensions.
Ranga Rodrigo. Class is central to object oriented programming.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
Implementation Yaodong Bi. Introduction to Implementation Purposes of Implementation – Plan the system integrations required in each iteration – Distribute.
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Class Library, Formatting, Wrapper Classes, and JUnit Testing
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 8 September 9, 2002.
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Module Road Map Refactoring Why Refactoring? Examples
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Introduction to JUnit CS 4501 / 6501 Software Testing
Factory Patterns 1.
Testing and Debugging.
Lecture 23 Polymorphism Richard Gesick.
Mocking Your Objects with Impunity
Overview of Eclipse Lectures
Introduction to JUnit CS 4501 / 6501 Software Testing
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Designing For Testability
CMSC 202 Exceptions.
Junit Tests.
Message Passing Systems
Presentation transcript:

Integration Testing When testing a module in isolation –Called modules need to be replaced –Tested module needs to be called A D ′ E ′ main driver module under test stubs

Drivers A driver is a method used to call a module that is being tested. –Sends values to module under test. – Collects return values JUnit test cases essentially are test drivers. The following could also be a test driver: public class TestSomething { public static void main ( String[] args ) { int result = 0; double send = 98.6; Whatever what = new Whatever; System.out.println( ″Sending someFunc ″ ); result = what.someFunc( send ); System.out.println( ″SomeFunc returned: ″ + result ); }

Stubs A stub is a module used specifically for testing that replaces a called module. Why? –To gain access to an interface that is otherwise not visible. –Actual module is not ready or reliable. –Actual module would be a component such as a database, network requiring a large amount of setup.

Creating a Stub The stub must have the same signature as the real module. –Same name, parameter list, return value type, modifiers ( static, public, etc.) Function: –If the module is called, check that the incoming parameters are as expected. –Provide suitable return values. –The stub should not attempt to replicate the functionality of the actual module. Instead, preset return values should be used.

Example of a Stub Method signature: public static int someMethod(int aParameter) A possible stub for this method public static int someMethod(int aParameter) { int result = 42; System.out.println (″In someMethod(): ″ + ″Input int = ″ + aParameter); System.out.println (″Returning 42.″); return result; }

Common Stub functions Display/log trace message Display/log passed parameters Return a value according to test objective. This could be determined from: –a table –an external file –based on a search according to parameter value

Stubs – multiple calls void main() { int x, y; // 1 x = TestMe1.a(); if (x > 0) { // 2 y = TestMe2.b(x); TestMe3.c(y); } else { // 3 TestMe3.c(x) } System.exit(0); // 4 } Test for path 1 – –Stub for TestMe1.a() needs to return x > 0 Test for path 1 – 3 – 4 – 6 – 7 –Stub for TestMe1.a() needs to return x <= 0 Stub will have to be able to return the appropriate values depending on the sequence of calls. Problem: stubs can become too complex

Mock Objects as Stubs A mock object is used as a substitute object in a situation where a stub is called for. Set up variables within objects to have interface types instead of specific class types. Multiple classes can implement the interface, if necessary. –In particular, a mock object class can also implement the interface, effectively making object instances as stubs. –Can be refined incrementally by replacing with actual code

Dependent class associations Many classes are set up so that associated classes are referred to specifically by their name, as a type for the association reference variable. –This makes it difficult to replace the dependent class. test class under test (CUT) dependent class referenced by CUT

Dependent Classes Client is the class which we would like to test. Helper is a class used by Client. Reasons for replacing Helper : –If it was not ready –We want direct control over what is sent to Client.

Goals: At present, class Client is tied to a specific type of object. –Modifications need to be made to Client if other types are used. We could use subclasses of Helper, but these subclasses still inherit behaviour from Helper. –This functionality may not be complete. –Does not provide for replacing Helper objects quickly. Alternative: use the interface mechanism.

Dependent class associations with interfaces Have classes store references using interface types instead of classes. –Mock object can then implement the interface with minimal functionality test class under test (CUT) interface referenced by CUT implementation class mock object

Example Client > IHelper Helper

Example Client > IHelper HelperMockHelper

Example of a mock object public class MockHelper implements IHelper { public MockHelper ( ) { } public Object helperMethod( Object aParameter ) { Object result = null; if ( ! aParameter.toString().equals("expected" ) { throw new IllegalArgumentException( "Unexpected parameter: " + aParameter.toString() ); } result = new String("reply"); return result; }

Class dependencies – object creation Another factor that can make using mock objects more difficult is when an object creates its dependent objects. –Using new ObjectClassName() to create an object still ties the associated object to a particular class, even if the reference is then assigned to an interface type. test class under test (CUT) object created by CUT

Strategies Design for testability –(but you tell the designers that they are designing for extensibility / maintenance ) Option 1: Use the “inversion of control” pattern. Option 2: Use the (Abstract) Factory pattern

Inversion of Control Pattern The “rule” for this pattern is that objects should not directly create any objects for which they might have an association. –If object A is supposed to work with object B, then A should not create B. –Instead, A and B should be created separately, and then they are passed references to each other.

Inversion of Control Pattern Rationale: –For future extensibility, the objective is to increase the independence of classes. –If object B is replaced by object B, version 2.0, class A doesn’t have to be changed. Rationale for testing: –We can replace “real” object B by a mock object for the purpose of testing object A.

Interfaces and inversion of control Class Client now has an association with IHelper instead of Helper. –Instead of having Client create a Helper object, a reference to an IHelper is passed in via an accessor method. A reference to IHelper is then stored within Client. Either a Helper or MockHelper can be created, and then passed to Client via the accessor. –A test case can create either the real or the mock object as appropriate.

Inversion of control

Factory Pattern Objective is to isolate the creation of any objects used in an application. Rationale: As class names change through software evolution, only the Factory needs to be updated. A class known as a “factory” will be responsible for any object creation, –Whenever the application needs a new object, ask the factory class to create the object. –Alternative: create a single Factory object from the Factory class.

Abstract Factory Pattern With this approach, the Factory is itself an interface, and there could be several implementations of the Factory. Rationale (by example): –An application may want to create a user interface button, and so it asks the abstract factory to create an object. –There could be three implementations of the Factory, one of which is currently active: –Windows (as in Microsoft) factory –X-Windows (for Linux, etc.) factory –Macintosh button factory Rationale for testing: –Substitute a Factory that creates mock objects.

UML diagram of Abstract Factory pattern

Example: Factory interface and classes public interface IFactory { IConverter createProduct(); } public class ProductionFactory implements IFactory { public IConverter createProduct( ) { return new Converter( ); // Creates the real object } public class MockFactory implements IFactory { public IConverter createProduct( ) { return new MockConverter( ); // Creates a mock object }

Automating Mock Object Creation What do we have to do to create a mock object? –Identify methods that can be called. –Create a few cases where we know the output for given input. –Identify the input as a known case. –Return the associated output. Objective: build a mock object automatically. –Structure can be determined using Java’s reflection capability. –Capability to allow user to specify the mock object’s behaviour easily needs to be provided.

EasyMock Framework for creating mock objects at run time. Uses Java reflection to create a mock object class that matches an interface. Open source project hosted on SourceForge –

Usage General steps: 1.Create the mock object 2.Tell the mock object how to behave when called. 3.Activate the mock object (this is called “replay” in the EasyMock documentation) 4.Run the test(s) 5.After the tests have run, check with the mock object to see if the expected calls were made.

Mock Object Creation A call to the EasyMock framework will generate a mock object. –Specify a Java object of type Class as the parameter to the method call. –Normally, this would be an interface. –At this point, you can specify whether the mock object is to have strict behaviour. –Normal: mock object will not enforce the ordering of calls to its methods. –Strict: calls to mock object methods must be in the sequence that is specified.

Example: Creation of Mock Object import org.easymock.EasyMock; private Client testMe; private IHelper public void setUp( ) { testMe = new Client( ); mock = EasyMock.createMock( IHelper.class ); testMe.setHelper( mock ); }

Specify the Mock Object behaviour Items to do: –Select a method in the mock object that could be called. –For that method: –Provide a set of input parameter values to be matched. –Provide a value to be returned when the method is called with those parameter values.

Specify the Mock Object behaviour Mechanism: –Option 1: If no value needs to be returned, just make the method call on the mock object with a parameter value that should be provided. –Option 2: If a value needs to be returned, use the EasyMock static method expect() with the method call (a Method object) as a parameter. –The return value is of a type that permits calling a method andReturn(). The parameter to that method will be the return value when the input matches the values that appear in the call to expect().

Set up mock object public void testCtoF_Freezing( ) { EasyMock.expect( mock.helperMethod( new String( ″expected″ ) ) ).andReturn( new String( ″reply″ ) ); // Activate mock object // Rest of test case } Case is when parameter matches this value. Called method When method is called with input parameter “expected”, return the string “reply”. Reference to mock object

Activating the Mock Object After specifying the mock object’s future behaviour, the next step is to activate it. With the active mock object, we can then run our tests that call the object under test that collaborates with the mock object.

Set up mock object behaviour, and run public void sampleTest( ) { EasyMock.expect( mock.helperMethod( new String( ″expected″ ) ) ).andReturn( new String( ″reply″ ) ); EasyMock.replay( mock ); String input = new String( ″expected″ ); Double expected = new String( ″reply″ ); Double actual = testMe.helperMethod( input ); assertEquals( expected, actual ); } Activate mock object Remainder of test is as usual.

After the test We want to be sure that the mock object actually received the expected calls. The EasyMock framework provides a verify() method that takes the mock object as a parameter. –If the expected calls occurred, the method will return. –If an expected call was missed, an exception will be thrown. –This exception will cause a JUnit failure.

Verification of calls to mock object private IHelper public void tearDown( ) { EasyMock.verify( mock ); }

Additional EasyMock Features Specifying a number of repeated calls with the same input parameters. Specifying that the mock object is to throw an exception when a method is called with a specified value. Specifying that a method should be called with specified values: –At least once –Between min and max number of times –Any number of times Pattern matching on input values, instead of equality.