Test doubles and EasyMock

36
Tests Doubles & EasyMock Created by Rafael Gutiérrez / / [email protected] @abaddon_gtz May 2016, JVMMX @ Nearsoft

Transcript of Test doubles and EasyMock

Page 1: Test doubles and EasyMock

Tests Doubles &EasyMock

Created by Rafael Gutiérrez / / [email protected]@abaddon_gtz

May 2016, JVMMX @ Nearsoft

Page 2: Test doubles and EasyMock

EasyMockEasyMock is the �rst dynamic Mock Object generator,

relieving users of hand-writing Mock Objects, or generatingcode for them.

Page 3: Test doubles and EasyMock

Mock?What is a Mock?

Are you mocking me?

Page 4: Test doubles and EasyMock

Mock

Page 5: Test doubles and EasyMock

WikipediaIn object-oriented programming, mock objects are simulatedobjects that mimic the behavior of real objects in controlled

ways.

Page 6: Test doubles and EasyMock

Googling: Mocks Aren't Stubs - Martin Fowler"However to fully understand the way people use mocks it is

important to understand mocks and other kinds of testdoubles."

Page 7: Test doubles and EasyMock

What is a Test Double!?Mocks and now Test Doubles!, really?!

Page 8: Test doubles and EasyMock

Test DoublesThey are also known as: Imposters.

SUT is a System under test.

DOC is a Depended-on Component.

Tests Doubles are like Stunt Doubles in �lming industry.

The movie is the SUT and the leading actor is the real DOC.

Page 9: Test doubles and EasyMock

Why do you need a test double?Basically, to isolate the code you want to test from its

surroundings

Page 10: Test doubles and EasyMock

Speed up test executionA test double’s implementation is often faster to execute than

the real thing.

A complex algorithm could take minutes to run, and minutesis FOREVER when we as developers want immediate

feedback by running automated tests.

You have to test those slow algorithms somewhere but noteverywhere.

Page 11: Test doubles and EasyMock

Make execution deterministicWhen your code (and tests) is deterministic, you can run your

tests repeatedly againts the same code and you will alwaysget the same result.

What happen when your code has nondeterministicbehaviour: random behaviour, time-depending behaviour?

Test doubles can lend a hand with these kinds of sources fornondeterministic behavior.

Page 12: Test doubles and EasyMock

Simulating special conditionsThere is always some conditions we can’t create using just the

APIs and features of our production code.

This happens when our code depends on a third party API,internet connection, an speci�c �le in some location, etc, etc.

Page 13: Test doubles and EasyMock

Exposing hidden informationWhat information? information about the interactions

between the code under test and its collaborators.

Encapsulation and information hiding is a good thing in yourcode but complicates testing.

You could (please don't!) add methods for testing purposes toyour production code. By substituting a test double for thereal implementation, you can add code for-testing-only and

avoid littering your production code.

Page 14: Test doubles and EasyMock

Types of Test Doubles

Page 15: Test doubles and EasyMock

Wait, before it is important to understand

Page 16: Test doubles and EasyMock

Test Stub (I)Also known as: Stub

With a Test Stub we can verify logic independently when theSUT depends on indirect inputs from other software

components.

We use a Test Stub when we need to control the indirectinputs of the SUT.

Page 17: Test doubles and EasyMock

Test Stub (II)There some variations:

A Responder is used to inject valid indirect inputs into theSUT.

A Saboteur is used to inject invalid indirect inputs into theSUT.

Other variations: Temporary Test Stub, Procedural Test Stub& Entity Chain Snipping

Page 18: Test doubles and EasyMock

Test Spy (I)Also know as: Spy or Recording Test Stub

With a Test Spy we can verify logic independently when theSUT has indirect outputs to other software components.

We use a Test Spy as an observation point to capture theindirect outputs for later veri�cation.

Page 19: Test doubles and EasyMock

Test Spy (II)There some variations:

Retrieval Interface is a Test Spy with an interface speci�callydesigned to expose the recorded information.

Self Shunt collapses the Test Spy and the Testcase Class intoa single object. When SUT calls DOC it is actually calling

methods in the Testcase Object.

Other variations: Inner Test Double & Indirect OutputRegistry

Page 20: Test doubles and EasyMock

Mock Object (I)With a Mock Object we can implement Behaviour

Veri�cation on the SUT.

We con�gure the Mock Object with the expected methodcalls and values with it should respond.

When exercising the SUT the Mock Object compares theactual with the expected arguments and method calls and

fails if they do not match.

No need for assertions in the test method!

Page 21: Test doubles and EasyMock

Mock Object (II)Mocks could be strict or nice.

Strict Mocks if the calls are received in a different order thanthe expected. Nice Mocks tolerates out-of-order calls.

Tests written using Mock Objects look different because allthe expected behavior must be speci�ed before the SUT is

exercised.

Page 22: Test doubles and EasyMock

Fake Object (I)Also know as: Dummy

We use a Fake Object to replace the component the SUTdepends with a much lighter-weight implementation.

The Fake Object only needs to provide the equivalentservices the real DOC provides.

Page 23: Test doubles and EasyMock

Fake Object (II)There some variations:

Fake Databases replace the persistent layer with a FakeObject that is functionally equivalent. When replacing the

database with in-memory HashTables or Lists.

In-Memory Database is a Dummy database with a small-footprint functionality.

Other variations: Fake Web Service & Fake Service Layer

Page 24: Test doubles and EasyMock

EasyMock by Example

Page 25: Test doubles and EasyMock

Create a Mock Object1. Create Mock Object

static <T> T EasyMock.mock(Class<T> toMock)

2. Record the expected behaviour

3. Switch the Mock Object to replay state

Example: UserServiceImplFindByIdTest

Page 26: Test doubles and EasyMock

Strick MocksWhen using EasyMock.mock() the order of method calls is

not checked.

Use EasyMock.strictMock() to create a Mock Objectthat check the order of method calls.

Example: UserServiceImplActivateTest

Page 27: Test doubles and EasyMock

Nice MocksMock Objects created with EasyMock.mock() will throw

AssertionError for all unexpected calls.

A Nice Mocks allows all method calls and returns appropriateempty values (0, null, false) for unexpected method calls.

Example: PaymentServiceImplChargeTest

Page 28: Test doubles and EasyMock

ExpectationsIn Record state the Mock Object does not behave like Mock

Object, but it records method calls.

Only after calling replay(), it behaves like Mock Objectchecking whether the expected method class are really done.

This means that in record state is where we specify what weexpect from the Mock Object.

Page 29: Test doubles and EasyMock

Behavior of method callsUse:

To return a IExpectationSetters which we can use tosetting expectations for an associated expected invocation.

static <T> IExpectationSetters<T> expect(T value) static <T> IExpectationSetters<T> expectLastCall()

Page 30: Test doubles and EasyMock

IExpectationSetters to answer,return or throw something

IExpectationSetters<T> andAnswer(IAnswer<? extends T> answer) IExpectationSetters<T> andReturn(T value) IExpectationSetters<T> andThrow(Throwable throwable)

Example:IExpectationSettersToAnswerReturnOrThrowTest

Page 31: Test doubles and EasyMock

IExpectationSetters to stubresponses

We stub to respond to some method calls, but we are notinterested in how often they are called, when they are called,

or even if they are called at all.

IExpectationSetters<T> andStubAnswer(IAnswer<? extends T> answer) IExpectationSetters<T> andStubReturn(T value) IExpectationSetters<T> andStubThrow(Throwable throwable)

Example: IExpectationSettersToStubTest

Page 32: Test doubles and EasyMock

IExpectationSetters to specify thenumber of calls and verify()

In EasyMock:

IExpectationSetters<T> times(int count) IExpectationSetters<T> times(int min, int max) IExpectationSetters<T> once() IExpectationSetters<T> atLeastOnce() IExpectationSetters<T> anyTimes()

static void verify(Object... mocks)

Example: IExpectationSettersNumCallsTest

Page 33: Test doubles and EasyMock

Expectations with Argument MatchersObject arguments are compared using equals() when

matching method calls in Mock Objects.

This could lead to some issues or maybe we could need amore �exible way to match method calls.

EasyMock class contains a lot of prede�ned argumentmatchers for us to use!

Example: EasyMockArgumentMatchersTest

Page 34: Test doubles and EasyMock

Capturing ArgumentsYou can capture the arguments passed to Mock Objects.

In EasyMock

static <T> T capture(Capture<T> captured) static x captureX(Capture<X> captured) // for primitives

Matches any value and captures it in the Capture parameterfor later access. You can also specify a CaptureType tellingthat a given Capture should keep the �rst, the last, all or no

captured values.

Example: CapturingArgumentsTest

Page 35: Test doubles and EasyMock

Mocks concrete Objects?Yes!

Page 36: Test doubles and EasyMock

Thanks!