mock objects and distributed testing -...

38
Mock Objects and Distributed Testing Mock Objects and Distributed Testing Making a Mockery of your Software Brian Gilstrap “Once,” said the Mock Turtle at last, with a deep sigh, “I was a real Turtle.” (Alice In Wonderland, Lewis Carroll)

Upload: doanhanh

Post on 10-Feb-2018

232 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Mock Objects and DistributedTesting

Mock Objects and DistributedTesting

Making a Mockery of your Software

Brian Gilstrap

“Once,” said the Mock Turtle at last, with a deep sigh,“I was a real Turtle.”

(Alice In Wonderland, Lewis Carroll)

Page 2: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

The AssumptionsThe Assumptions

• You need to test your code• You don't want to test by hand every time• Therefore: You need automated tests• But:

• Your software is too large to test effectivelyas a single unit

• Your software has classes/components whichcollaborate with each other

• Ergo: You need mock objects

Page 3: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

The Testing ProblemThe Testing Problem

• In complex systems, how do you test thepieces?

Recipe Ingredient

Instruction Utensil

How to testby itself?

Page 4: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Mock Objects: The IdeaMock Objects: The Idea

• Replace the real objects with substitutes,called Mock Objects• Not specific to Java

Recipe Ingredientmock

Instructionmock Utensil

Don't need ifnot used directly

Page 5: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Using Mock Objects to TestUsing Mock Objects to Test

• Test code 'conspires' with Mock objects tomake sure behavior of tested code meetsrequirements

Recipe

Ingredientmock

InstructionmockTestDriver

Page 6: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Principles of mock objectsPrinciples of mock objects

• Start small• Start by testing smallest possible units

• Build upon previous tests• Make tests automated

• Otherwise, you just won’t run them• Automated build systems can run them

• Tested object doesn’t know there are mocks

Page 7: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Interfaces are Your FriendInterfaces are Your Friend

• Easily replace normal implementation with mock• Tested object can be unaware talking to a mock

object

Ingredient

IngredientmockIngredientImpl

Recipe

Page 8: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Dependency Injection is yourFriend

Dependency Injection is yourFriend

• Prevents problems substituting the mockobject for the real one

• Must be careful to avoid hard-coding typesor directly instantiating dependencies• Avoid:

Ingredient ingredient = new SimpleIngredient(…);

Page 9: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Issue: Code doesn't useInterfaces

Issue: Code doesn't useInterfaces

• Implementing Mock objects is awkward• Have to sub-class and override methods• Hard to get right• Brittle• Sometimes impossible

• Generally indicates bad (or at least fragile) design ofthe object to be Mocked out

Page 10: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Issue: Dependency Resolutionis Hard-Coded

Issue: Dependency Resolutionis Hard-Coded

• No real solution without changing the code• Refactor code to use Dependency Injection• Singletons aren't the answer, they are part of

the problem

Page 11: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Mock Objects in the largeMock Objects in the large

• Scaling up to test larger collections of code• Component, Sub-system, module, service, etc.• It's a 'normal' unit test unless your code makes

remote calls• mostly (more later)

Page 12: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Mock Objects for DistributedSystems: Mockeries

Mock Objects for DistributedSystems: Mockeries

• Needed when testing distributed systems• Required to isolate & test a component• Normal testing techniques fail when testing

distributed apps• Local mock object doesn't test transport

• Common cause of subtle or software/version-specific bugs• Can change semantics

• Would like to skip testing but vendor's don't do allthey should

• So the developer has to do it instead

Page 13: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Mockeries (cont)Mockeries (cont)

• Many real-world testing situations need them:• Databases• EJBs• Servlets• CORBA• Java Connector Architecture (JCA)• Sockets• Etc. (e.g. these are just Java examples)

• Assumes you've already tested individualclasses/components

Page 14: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Example: WebApp MockeryExample: WebApp Mockery

• Is really a bit more complicated

Browser Servlet Database

Page 15: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp MockeryWebApp Mockery

Browser Servlet Database

Page 16: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp MockeryWebApp Mockery

Browser Servlet Container

RenderingEngine Servlet API

Servlet

DatabaseDatabaseEngineJDBC

Page 17: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp MockeryWebApp Mockery

Browser Servlet Container

RenderingEngine Servlet API

Servlet

Database

DatabaseEngine

HTTP HTTPJDBC

Page 18: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

OS

OS

OS

WebApp MockeryWebApp Mockery

Browser Servlet Container

RenderingEngine Servlet API

Servlet

Database

DatabaseEngine

HTTP HTTPJDBC

TCP TCP TCP

Page 19: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp MockeryWebApp Mockery

OS

OS

OS

Browser Servlet Container

RenderingEngine Servlet API

Servlet

Database

DatabaseEngine

HTTP HTTPJDBC

TCP TCP TCP

IP IP IP

Page 20: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp MockeryWebApp Mockery

OS

OS

OS

Browser Servlet Container

RenderingEngine Servlet API

Servlet

Database

DatabaseEngine

HTTP HTTPJDBC

TCP TCP TCP

IP IP IP

Ethernet Ethernet Ethernet

Page 21: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp MockeryWebApp Mockery

Wire

OS

OS

OS

Browser Servlet Container

RenderingEngine Servlet API

Servlet

Database

DatabaseEngine

HTTP HTTPJDBC

TCP TCP TCP

IP IP IP

Ethernet Ethernet Ethernet

Page 22: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp Mockery: TestingOpportunities

WebApp Mockery: TestingOpportunities

Wire

OS

OS

OS

Browser Servlet Container

RenderingEngine Servlet API

Database

DatabaseEngine

HTTP HTTPJDBC

TCP TCP TCP

IP IP IP

Ethernet Ethernet Ethernet

Servlet

Page 23: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

WebApp Mockery:Breaking it Down

WebApp Mockery:Breaking it Down

• Lots of things we could simulate• Start Simple• Expand tests as

• Time allows• Bugs require

• Hard to test entire path with simple mocks

Page 24: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Mockeries versus normal Mocks:Distance Matters

Mockeries versus normal Mocks:Distance Matters

• New failure modes• Communication failure• Service/component failure• Transport errors

• New sources of existing failure modes• Race conditions

• More likely• Deadlock

• Much harder to diagnose• Bad dependency resolution (talking to a wrong/missing

object)• Much more likely

Page 25: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Distributed Testing TradeoffsDistributed Testing Tradeoffs

• Eliminate distribution to test correctness• Make sure to preserve pass-by-value/reference

semantics of transport

• Use pre-planned requests and results to testdistribution/timing issues

• Use random tests to search for subtle bugs

Page 26: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Distributed Testing TradeoffsDistributed Testing Tradeoffs

• Eliminate distribution to test correctness• Make sure to preserve pass-by-value/reference semantics of

transport

Wiremock

OS

Browsermock Servlet Containermock

Servlet APImock

Servlet

Databasemock

HTTPmockJDBCmock

TCPmock

IPmock

Ethernetmock

Page 27: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Servlet Containermock

Servlet APImock

Servlet

HTTPmockJDBCmock

TCPmock

IPmockOS

Servlet ContainerServlet

Databasemock

Distributed Testing TradeoffsDistributed Testing Tradeoffs

• Use pre-planned requests and results to testdistribution/timing issues

OS

OS

Browsermock

HTTP

TCP TCP

IP IP

Ethernet Ethernet

Servlet API

HTTPJDBC

TCP

IP

Ethernet

Wire

Page 28: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Distributed Testing TradeoffsDistributed Testing Tradeoffs

• Use random tests to search for subtle bugs• Especially good for deadlock/race conditions• Worth their weight in 'gold' (development time)

in complex systems• Requires you know how to recognize errors

• Request/response is predetermined• Logging indicates errors• Crash or corrupted data leaves no doubt

• Hardest to analyze

Page 29: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Divide and conquerDivide and conquer

• Build up confidence with layers of tests• Basic Correctness - unit tests

• Remove distribution• One component at a time

• Timing• Simulated and real distribution• Force multi-threaded situations

• Resilience - test specifically for handling of failure modes• Network trouble• Remote component goes away• Incorrect results from called services

• Discovery• Doesn’t generally occur in standalone programs

Page 30: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

An Example: Music DatabaseAn Example: Music Database

• Artists• Name• Albums produced

• Albums• Tracks

• Tracks• Title• Track number

Page 31: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Music Database: InterfacesMusic Database: Interfaces

Wire

OS

OS

OS

Browser Servlet Container

RenderingEngine Servlet API

Database

DatabaseEngine

HTTP HTTP

TCP TCP

IP IP IP

Ethernet Ethernet Ethernet

Servlet

model

JDBC

TCP

Page 32: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Music Database: Test the modelMusic Database: Test the model

JUnit

DatabaseEngine

model

JDBC

Page 33: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

JUnit

Music Database: Test the servletMusic Database: Test the servlet

HttpUnitServlet

Model APIMockModel

Page 34: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Music Database: Where next?Music Database: Where next?

• Multi-threaded test (concurrency)• No servlet, just model• Mock model, just servlet

• Resource management test• JDBC connection management• Mock driver that tracks open connections,

statements, etc.• Fails if any left open after executing test

• Etc.

Page 35: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Making a MockeryMaking a Mockery

• Determine what you want to test:• Correctness of component logic• Correctness of transport/marshalling• Timing• Deadlock

• Re-use existing mock frameworks if possible• Build mocks if you have to• Strive to simulate the real world

• e.g. don’t try to test for timing issues on a single-CPUsystem

Page 36: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

Mockeries: ConclusionMockeries: Conclusion

• "I found it in the last place I looked"• Bugs are in the code/situations we haven't tested

• Doesn't this go beyond ‘unit’ testing?• Yes• If you've implemented unit tests, remaining bugs aren't

caught through normal unit tests• Not always easy to implement

• Much easier than thrashing about with one-off tests orcode inspection

• Don't throw away effort required to reproduce thebug!• Capture it in a Mockery

Page 37: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

ReferencesReferences

• Original Paper• http://www.connextra.com/aboutUs/mockobjects.pdf

• General Sites• http://www.mockobjects.com

• Articles• http://www.ociweb.com/jnb/jnbJun2003.html• http://www-106.ibm.com/developerworks/library/j-

mocktest.html

Page 38: Mock Objects and Distributed Testing - java.ociweb.comjava.ociweb.com/javasig/knowledgebase/2005-10/Mockeries.pdf · Mock Objects and Distributed Testing Making a Mockery of your

More ReferencesMore References

• Frameworks• https://www.mockobjects.com/• https://www.easymock.org• http://httpunit.sourceforge.net/• http://www.hsqldb.org/• http://jdbcunit.sourceforge.net/• Etc.

• Books• Pragmatic Unit Testing (Pragmatic Programmers)• Unit Testing in Java: How Tests Drive the Code

• <Soapbox>Test-driven development is good; not so sure abouttest-first development</Soapbox>

• Lots more