(c) 2002 Object Mentor, Inc.1 Testing Things That Seem Hard To Test Robert Koss, Ph. D. ObjectMentor, Inc.

Slides:



Advertisements
Similar presentations
3 Copyright © 2005, Oracle. All rights reserved. Designing J2EE Applications.
Advertisements

CS0004: Introduction to Programming Visual Studio 2010 and Controls.
Object-Oriented Analysis and Design CHAPTER 17, 25: GRASP PATTERNS 1.
Feb R. McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m Function:
Layered Architectures The objectives of this chapter are: To understand the principles and importance of Layered Architectures To explain the structure.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Web Servers How do our requests for resources on the Internet get handled? Can they be located anywhere? Global?
Object-Oriented Analysis and Design
Chapter 7 Improving the User Interface
Threads and GUIs Confession: I’ve never liked GUI programming very much – so I haven’t done a lot of it  I don’t like the visual design aspects  But.
March R McFadyen1 Figure 30.2 Layers in NextGen They only have three layers in this architecture Each layer is shown as a UML Package No separate.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Web Application Architecture: multi-tier (2-tier, 3-tier) & mvc
Software Development Architectures Ankur K. Rajopadhye Louisiana Tech University.
UNIT-V The MVC architecture and Struts Framework.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Introduction to the Enterprise Library. Sounds familiar? Writing a component to encapsulate data access Building a component that allows you to log errors.
The Design Discipline.
Chapter 13 Starting Design: Logical Architecture and UML Package Diagrams.
Systems Analysis and Design in a Changing World, Fifth Edition
BEST PRACTICES - Java By Configuration Use global-forwards/results Helps to avoid duplicate jsp files and redundancy forward mapping.
@benday #vslive Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
CS 0004 –Lecture 1 Wednesday, Jan 5 th, 2011 Roxana Gheorghiu.
Model View Controller (MVC) Rick Mercer with a wide variety of others 1.
14 Chapter 11: Designing the User Interface. 14 Systems Analysis and Design in a Changing World, 3rd Edition 2 Identifying and Classifying Inputs and.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
CSE 303 – Software Design and Architecture
Welcome to CIS 083 ! Events CIS 068.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Click your mouse to continue. Ways to Merge Data When you have finished editing your main document and inserting the merge fields, you have several choices.
12 Systems Analysis and Design in a Changing World, Fifth Edition.
Putting together a complete system Chapter 10. Overview  Design a modest but complete system  A collection of objects work together to solve a problem.
Lecture 5: Using Computers: Important Ideas Tonga Institute of Higher Education IT 141: Information Systems.
1 Another group of Patterns Architectural Patterns.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 17 GRASP: Designing Objects with Responsibilities. 1CS6359 Fall 2011 John Cole.
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.
1 Implementation support chapter 8 programming tools –levels of services for programmers windowing systems –core support for separate and simultaneous.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
GRASP: Designing Objects with Responsibilities
Architectural Patterns Support Lecture. Software Architecture l Architecture is OVERLOADED System architecture Application architecture l Architecture.
Swing MVC Application Layering A Layer is a collection of components that Perform similar tasks. Perform similar tasks. Isolate implementation details.
Model View Controller A Pattern that Many People Think They Understand, But Has A Couple Meanings.
Model View Controller MVC Web Software Architecture.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Mock objects.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Creating a GUI Class An example of class design using inheritance and interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Model View Controller (MVC) an architecture Rick Mercer with help from many of others 1.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 6: Restaurant.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Test Isolation and Mocking Technion – Institute of Technology Author: Gal Lalouche © 1 Author: Gal Lalouche - Technion 2016 ©
Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Intro To MVC Architecture.
Model View Presenter Design Pattern Jay Smith PMO Architect and Evangelist Tyson Foods, Inc.
Designing For Testability
MVC and other n-tier Architectures
Model-View-Controller (MVC) Pattern
Starting Design: Logical Architecture and UML Package Diagrams
Exception Handling and Event Handling
Tonga Institute of Higher Education
Designing For Testability
11. MVC SE2811 Software Component Design
11. MVC SE2811 Software Component Design
Presentation transcript:

(c) 2002 Object Mentor, Inc.1 Testing Things That Seem Hard To Test Robert Koss, Ph. D. ObjectMentor, Inc.

(c) 2002 Object Mentor, Inc.2 Introduction XP dictates that we test everything that can possibly break –Subjective accessors and mutators What if mutator does validation? How much logic is too much? What if class Foo is difficult to test? FooTestFoo

(c) 2002 Object Mentor, Inc.3 Sources of Difficulty Some objects appear to be difficult to test –Objects which depend upon other objects. –GUI’s –Databases –Servlets / EJB’s What makes these things hard to test? –Not knowing what to test –Source code dependencies Collaborating objects

(c) 2002 Object Mentor, Inc.4 Knowing What to Test GUI –Assume Swing / MFC works –Model-View-Controller Make sure anything that can break is in a class which can be tested Databases –Assume Oracle / SQL Server, etc. works Servlets –HttpUnit –Delegate to a class which can be tested.

(c) 2002 Object Mentor, Inc.5 Collaborating Objects Mark IV Coffee Maker public void testMakeCoffee() { CoffeeMaker cm = new CoffeeMaker(); cm.makeCoffee(); assert( ? ? ? ); }

(c) 2002 Object Mentor, Inc.6 Breaking Troublesome Dependencies A source-code dependency can always be broken by the judicious use of an interface. Dependency has been inverted –Both A and B now depend upon an interface A no longer knows concrete type of B –Can now test A by supplying a MockB

(c) 2002 Object Mentor, Inc.7 Mock Objects / Stubs Class A can’t tell if message goes to class B or class BStub Class BStub used to create testing environment, totally under control of class ATest

(c) 2002 Object Mentor, Inc.8 Self-Shunt Pattern Not always necessary to make a new class and create a new object for Bstub Class ATest can implement the IB interface

(c) 2002 Object Mentor, Inc.9 CoffeeMakerTest Stub out Warmer and Boiler

(c) 2002 Object Mentor, Inc.10 Stubs Everywhere ? Stubs are good when real class can’t easily be used –Hardware –Database –Dependent class not done Not necessary for Value Objects Not necessary if dependants are known to work

(c) 2002 Object Mentor, Inc.11 Chains of Dependencies Bowling Game Boom, Splash, (x, y, z, t)

(c) 2002 Object Mentor, Inc.12 More Uses for Stubs Logging –When object under test is supposed to send a sequence of messages to another object in a specific order –Record message sent as a string append to string for each message –Alternative to setting several flags and examining in test

(c) 2002 Object Mentor, Inc.13 More Uses for Stubs Null Object Pattern –When doing nothing is the right thing to do –Don’t have to test for presence of Logger object

(c) 2002 Object Mentor, Inc.14 Testing GUI’s Test Model (MVC) first –Concentrate on business instead of user interface –Start just inside the UI –Quicken addCheck( new Check( checkNum, payee, amount)) is more important than a fancy check gui Same with reconcile()

(c) 2002 Object Mentor, Inc.15 Testing GUI’s Decide what to test –Can test everything except for aestetics –Should we? public void testWidgetsPresent() { SearchPanel panel = new SearchPanel(); assertNotNull(panel.searchLabel); assertNotNull(panel.queryField); assertNotNull(panel.findButton); assertNotNull(panel.resultTable); }

(c) 2002 Object Mentor, Inc.16 Being Pragmatic Assume GUI toolkit works –Swing, MFC, Qt, GTK –new Button( “OK”); Continually ask if code being written can possibly break Continually ask if code being written is doing more than UI Make sure all logic is in classes that can be tested Make GUI code so thin that it can’t possibly break.

(c) 2002 Object Mentor, Inc.17 Separate Domain From User Interface Model - View - Controller Document - View –Combines traditional View with Controller –Good unless several Controllers are needed Model - View - Presenter – 106.ibm.com/developerworks/library/mvp.html –Combines traditional View with Controller –Adds another layer between UI and Model –Presenter is ideal place to do unit testing of GUI

(c) 2002 Object Mentor, Inc.18 Single Responsibility Principle A class has a single responsibility. It meets that responsibility, the whole responsibility, and nothing but that responsibility –Called cohesion in Structured Design Applied to functions –Rule of thumb Describe class’s responsibility without using “and” What is the responsibility of an event handler? –“Handles” event –Scope of “handle” subjective –Should not have any program logic

(c) 2002 Object Mentor, Inc.19 A Stupid GUI Is an Unbreakable GUI Event handler should do nothing but gather the information from event and delegate responsibility to another class –Can’t possibly break –e.g., MouseEventHandler should get click coordinates and delegate to another class that knows what to do when the mouse is clicked hit testing

(c) 2002 Object Mentor, Inc.20 Event Handlers Dialog / Window with GUI Control and registered listener Testing listener can be –Easy / Annoying –Hard / Impossible How do we test this?

(c) 2002 Object Mentor, Inc.21 Event Handlers Delegate Have event handler delegate to something that can be tested Presenter (MVP)

(c) 2002 Object Mentor, Inc.22 General Architecture Note the direction of the dependencies Make this unbreakable

(c) 2002 Object Mentor, Inc.23 Interacting GUI Components Interaction with one GUI component causes changes to other components. –Menus becoming active / inactive –Toolbar changes Mediator Pattern –Have each event handler delegate to mediator and have mediator determine what else must change –Component interactions now localized (and testable!) instead of being distributed over all event handlers.

(c) 2002 Object Mentor, Inc.24 Mediators as State Machines Using a Mediator localizes inter-object communication Mediators can become ugly for complicated windows Implementing Mediator as a state machine makes it easy to get the wiring correct and to easily accommodate changes

(c) 2002 Object Mentor, Inc.25 Summary We have to test everything that can possibly break If something appears hard to test, don’t let it do anything that can break –Delegate to an object that can be tested