Mock Objects in the Smalltalk World Dennis Schetinin.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Computer-aided mechanism design Ye Fang, Swarat Chaudhuri, Moshe Vardi 1.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Smalltalk Connections 1 Test Driven Development (TDD) Presented by Victor Goldberg, Ph.D.
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
1 Software Testing and Quality Assurance Lecture 14 - Planning for Testing (Chapter 3, A Practical Guide to Testing Object- Oriented Software)
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Applied Software Project Management 1 Introduction Dr. Mengxia Zhu Computer Science Department Southern Illinois University Carbondale.
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
Test-Driven Development “Test first, develop later!” –OCUnit.
Living Requirements using Behavior Driven Development
Domain Modeling (with Objects). Motivation Programming classes teach – What an object is – How to create objects What is missing – Finding/determining.
By Bob Bunson  Simulation of software development project  Fictitious system from Concept to Code  Oriented around the.
TDD,BDD and Unit Testing in Ruby
Refactoring Lecture 5 CIS 6101 Software Processes and Metrics.
Exploring Users’ Values, Motivations and Emotions Sarah Thew University of Manchester
CENG 311 Machine Representation/Numbers
1 Design and Integration: Part 1 Nuggets about Design vs Project Management.
Behaviour Driven Development with Cucumber for Java.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Microsoft ® Office 2007 Training Security II: Turn off the Message Bar and run code safely presents:
Intro to Architecture – Page 1 of 22CSCI 4717 – Computer Architecture CSCI 4717/5717 Computer Architecture Topic: Introduction Reading: Chapter 1.
Testing Strategy Big Picture Planning for Software Testing.
Clear Lines Consulting · clear-lines.comSilicon Valley Code Camp 2008 · Nov 8, 2008 · 1 Test-Driven Development An introduction for C# developers.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Software testing Main issues: There are a great many testing techniques Often, only the final code is tested.
Conflict in Team Environments – Part 2 Professional Year Program - Unit 6: Communicating in work teams to achieve professional goals.
Nested Mutex Design Document and Proposed Solution.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Black-box Testing.
STOP UNIT TESTIN G. * * Sort of… STOP UNIT TESTIN G * * Sort of… Alec Wojciechowski Senior Consultant ILM Professional Services
GoF Sections Design Problems and Design Patterns.
Programming Life Cycle Problem analysisunderstand the problem Requirements definition specify what program will do High- and low-level designhow it meets.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
Using Mock Objects with Test Driven Development Justin Kohlhepp
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 Design and Integration: Part 2. 2 Plus Delta Feedback Reading and lecture repeat Ambiguous questions on quizzes Attendance quizzes Boring white lecture.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
A tool for test-driven development
Mock objects.
Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005.
Test-Driven Development Eduard Miric ă. The problem.
Artificial Intelligence in the Robotic Industry By Dalia Elzeny Jason Renaud.
Build Robust Web Apps in the Real WakeUpAndCode.com.
1 Presentation Title Test-driven development (TDD) Overview David Wu.
Lesson - 2. Introduction When we make a program we must follow some steps, called Programming Development Life Cycle (PDLC). Programming steps are five:
Test Case Designing UNIT - 2. Topics Test Requirement Analysis (example) Test Case Designing (sample discussion) Test Data Preparation (example) Test.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
(1) Test Driven Development Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Lecture 7 Conditional Scripting and Importing/Exporting.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
(Further analysis and Refactoring) Larman, chapters 23 and 24 Glenn D. Blank, CSE432.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Introduction to Software Engineering Muhammad Nasir Agile Software Development(2)
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
VIDEO TEXT RISK APPLICATION SPEED INTERNET DATA INFORMATION CONSUME BUSINESS CAPITAL RESOURCE VIDEO MEDIA ECONOMIC DIVERSE YES NO TRAINING TOWER COMMERCIAL.
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Designing For Testability
Chapter 8 – Software Testing
If, else, elif.
Introduction to Binary
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Presentation transcript:

Mock Objects in the Smalltalk World Dennis Schetinin

 Why Mock Objects?  “Mockist” vs. “Classic” TDD  “Mockist” and “ Classic” TDD  Mocks and Smalltalk:  The Mocketry framework introduction  Examples Overview 1 Mock Objects and Smalltalk

Do we need Mocks at all (in Smalltalk)? Why Mock Objects? 2 Mock Objects and Smalltalk

 Smalltalk vs. Mock Objects?  Few/rare special cases  With mock you don’t test real thing  Use mocks for external objects only  Use other means to involve complex external objects  Speed up by other means Public Opinion 3 Do we need Mock Objects at all?

…seems to be about testing Public Opinion 4

 “Mock Objects” is a TDD technique  … about developing systems  … not just testing  … useful in all languages  Smalltalk makes mocks  much easier to use Smalltalk and Mock Objects 5 Why Mock Objects?

 Cut off dependencies in tests  Test-Driven Decomposition  Discover Responsibility (for collaborators)  Thinking vs. Speculating/Fantasizing Seamless TDD Why Mock Objects? 6

 Dependencies  How to cut them off?  Novel Collaborators  Where to cut? What Is The Problem? 7 Seamless TDD

 Dependencies  How to cut them off?  Novel Collaborators  Where to cut? What Is The problem? 8 Seamless TDD

We have:  System Under Development (SUD)  Collaborators  Collaborators’ collaborators … Complex Test Dependencies 9 Seamless TDD — What’s the Problem?

We have to implement collaborators  … without tests  … loosing focus on SUD Digression So What? 10 Seamless TDD — Dependencies

 Mocks Aren’t Stubs by Martin Fowler  Filling orders from warehouse Dependencies: Example 11 Seamless TDD — Dependencies

OrderTests >> testIsFilledIfEnoughInWarehouse testIsFilledIfEnoughInWarehouse | order | order:= Order on: 50 of: #product. order fillFrom: warehouse. self assert: order isFilled Filling Order 12 Seamless TDD — Dependencies

OrderTests >> testIsFilledIfEnoughInWarehouse | order warehouse | warehouse := Warehouse new. “Put #product there” “…but how?!” order := Order on: 50 of: #product. order fillFrom: warehouse. … … Filling Order 13 Seamless TDD — Dependencies

 I develop Order  I don’t want to think about Warehouse Digression Detected! 14 Seamless TDD — Dependencies

OrderTests >> testIsFilledIfEnoughInWarehouse | order warehouse | warehouse := Warehouse new. warehouse add: 50 of: #product. order := Order on: 50 of: #prod. order fillFrom: warehouse. … Filling Order 15 Seamless TDD — Dependencies

Reduce amount of #product at the warehouse test…… self assert: (warehouse (warehouse amountOf: #product) amountOf: #product) isZero isZero …And Even More Digression 16 Seamless TDD — Dependencies

Another test case: If there isn’t enough #product in the warehouse,  do not fill order  do not remove #product from warehouse … And Even More Digression 17 Seamless TDD — Dependencies

More complex test cases Collaborators’ logic becomes more and more complex… This can engulf … Much More Digression 18 Seamless TDD — Dependencies

 SUD is Order  Warehouse blures SUD  #add:of:  #amountOf:  No explicit tests for Warehouse Not-So-Seamless TDD 19 Seamless TDD — Dependencies

OrderTests >> testIsFilledIfEnoughInWarehouse testIsFilledIfEnoughInWarehouse | order | | order | order := Order on: 50 of: #product. order := Order on: 50 of: #product. [ [ [ [ :warehouse| :warehouse| [order fillFrom: warehouse] [order fillFrom: warehouse] should satisfy: should satisfy: [“expectations”] [“expectations”] ] runScenario. ] runScenario. self assert: order isFilled self assert: order isFilled Mocking Warehouse 20 Seamless TDD — Dependencies

…[:warehouse| [order fillFrom: warehouse] should satisfy: should satisfy: [(warehouse [(warehouse has: 50 of: #product) has: 50 of: #product) willReturn: true. willReturn: true. warehouse warehouse remove: 50 of: #product] remove: 50 of: #product] ] runScenario. … Mocking Warehouse 21 Seamless TDD — Dependencies

Mock Objects in Smalltalk World The Mocketry Framework 22 Mock Objects and Smalltalk

When you do this with SUD, expect that to happen with collaborators Collaborators are mocked Behavior Expectations 23 Mocketry

Do this Expect that Behavior Expectations 24 Mocketry ExerciseVerify Scenario

SomeTestCases >> testCase [testScenario ] runScenario Mocketry Scenario Pattern 25 Mocketry – Behavior Expectations

SomeTestCases >> testCase [[exercise] should strictly satisfy: [behaviorExpectations] ] runScenario Mocketry Scenario Pattern 26 Mocketry – Behavior Expectations

 Just send mock objects the messages they should receive warehouse has: 50 of: #product has: 50 of: #product  Specify their reaction (warehouse has: 50 of: #product) has: 50 of: #product) willReturn: true willReturn: true Behavior Expectations 27 Mocketry — Behavior Expectations

SomeTestCases >> testCase [ [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry Scenario Pattern 28 Mocketry – Behavior Expectations

SomeTestCases >> testCase [ : mock | [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry Scenario Pattern 29 Mocketry – Behavior Expectations

SomeTestCases >> testCase [ :mock | [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry Scenario Pattern 30 Mocketry – Behavior Expectations

SomeTestCases >> testCase [ :mock1 :mock2 :mock3 | [exercise] should strictly satisfy: [behaviorExpectations] … do anything ] runScenario Mocketry Scenario Pattern 31 Mocketry – Behavior Expectations

TrueTests >> testDoesNotExecuteIfFalseBlock [ :block | [true ifFalse: block ] should satisfiy: [“nothing expected”] ] runScenario Trivial Example 1 32 Mocketry – Behavior Expectations

TrueTests >> testExecutesIfTrueBlock [ :block | [true ifTrue: block] should satisfiy: [block value] ] runScenario Trivial Example 2 33 Mocketry – Behavior Expectations

 resultObject should  resultObject should  result should be: anotherObject  result should equal: anotherObject ………… State Specification DSL 34 Mocketry

 There is much more…  Ask me  …or Dennis Kudryashov (the Author) Mocketry 35

OrderTests >> testIsFilledIfEnoughInWarehouse | order | order := Order on: 50 of: #product. [:warehouse| [order fillFrom: warehouse] [order fillFrom: warehouse] should satisfy: should satisfy: [(warehouse has: 50 of: #product) [(warehouse has: 50 of: #product) willReturn: true. willReturn: true. warehouse remove: 50 of: #product] warehouse remove: 50 of: #product] ] runScenario. self assert: order isFilled Mocking Warehouse 36 Seamless TDD — Dependencies — Example

OrderTests >> testIsNotFilledIfNotEnoughInWarehouse | order | order := Order on: #amount of: #product. [:warehouse| [order fillFrom: warehouse] [order fillFrom: warehouse] should satisfy: should satisfy: [(warehouse has: 50 of: #product) [(warehouse has: 50 of: #product) willReturn: false. willReturn: false. “Nothing else is expected” ] “Nothing else is expected” ] ] runScenario. self assert: order isFilled Mocking Warehouse 37 Seamless TDD — Dependencies

 No need to implement Warehouse  Just specify expectations  … right in the test  Focus on the SUD What We Get 38 Why Mock Objects

 Dependencies  Novel Сollaborators What’s the problem? 39 Seamless TDD

Where to Start? 40 Seamless TDD — Novel Collaborators A novel system to implement Object 1 Object 2 Object 3 Object N …

Where to Cut? 41 Seamless TDD — Novel Collaborators A novel system to implement Feature

 Try to guess  … and be ready to abandon test(s)  … or get a mess Or  Analyze thoroughly  … up-front decomposition  … without tests — just a fantasy Where to Start? 42 Seamless TDD — Novel Collaborators

Bulls and Cows Game  Computer generates a secret key  e.g., a 4-digit number  Human player tries to disclose it Novel Collaborators: Example 43 Seamless TDD — Novel Collaborators

Scenario:  User creates a game object  User starts the game  Game should generate a key ………… Bulls and Cows Game 44 Seamless TDD — Novel Collaborators

self assert: key …? self assert: key …? “How to represent key?!” “How to represent key?!” testGeneratesKeyOnStart 45 Seamless TDD — Novel Collaborators

 Spontaneous representation  Do you feel lucky?  Analyze thoroughly  Give up TDD  Postpone the test  Not a solution What Can I Do? 46 Seamless TDD — Novel Collaborators

 …  Create a new class for key  Unnecessary complexity? What Can I Do? 47 Seamless TDD — Novel Collaborators

That was a Digression ! What Can I Do? 48 Seamless TDD — Novel Collaborators

|key| game start. key := game key. self assert: key isKindOf: Code testGeneratesKeyOnStart 49 Seamless TDD — Novel Collaborators

[ :keyGen | game keyGenerator: keyGen. game keyGenerator: keyGen. [ game start ] [ game start ] should satisfy: should satisfy: [keyGen createKey [keyGen createKey willReturn: #key] willReturn: #key] game key should be: #key game key should be: #key ] runScenario. testGeneratesKeyOnStart 50 Seamless TDD — Novel Collaborators

 Key generation functionality  is revealed  moved to another object  Dependency Injection  fake key can be created for tests  KeyGenerator refactored to Turn  No risk of incorrect decision What We Get 51 Seamless TDD — Novel Collaborators

Seamless TDD:  No digression  No up-front decomposition  No up-front design  No speculating / fantasizing What We Get 52 Seamless TDD — Novel Collaborators

Mock Objects For Top-Down Design by Bulls-and-Cows Example Just ask! Complete Example 53

State vs. Behavior? Result vs. Intention? No contradiction! Mockist approach complements “classic” TDD Classic vs. Mockist TDD 54 Seamless TDD

 Top-Down with Mockist TDD  Analysis and Decomposition  Bottom-Up with Classic TDD  Synthesis and “real-object” testing Classic and Mockist TDD 55