don't give up on mocking - · pdf filedon’t give up on mocking-why do people give...

52
Don’t give up on mocking - Why do people give up? - Mocking: the big step from classic way of testing - Let’s take a step back and don’t give up! by Szczepan Faber (a certified mock tamer)

Upload: lamthu

Post on 10-Feb-2018

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Don’t give up on mocking

- Why do people give up?

- Mocking: the big step from classic way of testing

- Let’s take a step back and don’t give up!

by Szczepan Faber (a certified mock tamer)

Page 2: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Interaction testing...

� State testing is asking: „what’s your colour, Mr Object?”

� Interaction testing is asking: „Mrs Object, what did you say to Mr Object?”

Page 3: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

The language

� The natural language of state testing are assertions

� The natural language of interaction testing is... mocking?

Page 4: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

What’s a mock or a stub?

� It is a substitue of the real thing for the purposes of testing

Page 5: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mocking...

� Is it a design tool for describing messaging patterns between abstract state machines?

� Is it a handy tool which lets me create mocks dynamically?

Page 6: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Giving up...

� The internet says mocking is cool

� Let’s find out why one would give up on mocking!

Page 7: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Why would one give up on mocking?

Page 8: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Why would one give up on mocking?

� because aggressive validation makes the tests brittle �

Page 9: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

The code

Page 10: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

The test

Page 11: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

And the lovely green bar

Page 12: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

TDD-ing a new feature (test)

Page 13: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

The adorable red bar

Page 14: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

TDD-ing a new feature (code)

Page 15: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Whoah? Red bar again?

Page 16: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Why would one give up on mocking?

� because I have to fix tests even when the code is not broken:

� may increase noise

� may lead to overspecification

Page 17: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Fixing by ignoring interactions

Page 18: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Fixing by adding required expectation

Page 19: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Why would one give up on mocking?

� What if hand mocks were better?

Page 20: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Remember the code?

Page 21: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Let’s try some hand written mocks

Page 22: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

By hand or with the framework: the essence

Page 23: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

By hand or with the framework: expectations

Page 24: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Complete test

Page 25: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Why would one give up on mocking?

� Let’s look at the point of failure

Page 26: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Point of failure and hand mocks

Hand mocks show useful stack trace pointing to exact line of code

Page 27: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

When the framework fails on verify()

The exception message which tries to be readable.

Page 28: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

When the framework fails with

„Unexpected Interaction!”

Helpful but...

Page 29: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Ok, now I understand why one would

give up on mocking.

� because aggressive validation makes the tests brittle �

� because I have to fix tests even when the code is not broken

� but it can increase noise

� or lead to overspecification

� because hand-mocks can be considered better:

� less noisy

� more natural

� with better(?) point of failure

Page 30: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Are hand mocks a better option, then?

� Err... not really... hand mocks have different issues.

� Hand mocks bad, mocking framework bad what should I do now?

Page 31: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

A taste of Mockito, a Test Spy framework

Page 32: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

A taste of hand mocks, no framework at all

Page 33: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Test Spy framework

� because aggressive validation makes the tests brittle �

� because I have to fix tests even when the code is not broken

� but it can increase noise

� or lead to overspecification

� because hand-mocks can be considered better:

� less noisy

� more natural

� with better(?) point of failure

Page 34: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Languages, where are your Test Spy

frameworks?

� You’ve got plenty of mocking frameworks

� Java

� C#

� Ruby

� Python

� JavaScript

� But you’ve got so little Test Spy frameworks� Java

� C#

� Ruby

� Python

� JavaScript

Page 35: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

This is what is trendy in the mocking

world these days

� Better and better DSLs for describing expectations

� Partial mocking

� Mocking static methods

� Features that solve rare corner cases

� Etc.

Page 36: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mock objects: the quest for quality

� Does application code quality vary when using different mock libraries (or hand mocks)?

� Does test code quality vary when using different mock libraries (or hand mocks)?

� Can I use different mock libraries in single project?

Page 37: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mocking in Java

� jMock

� EasyMock

� Mockito

Page 38: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

How to verify the method was called?

JMock:

EasyMock:

Mockito:

Page 39: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

How to tell a method to return a value?

JMock:

EasyMock:

Mockito:

Page 40: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

How verify the method was not called

JMock:

EasyMock:

(always implicit)

Mockito:

Page 41: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mockito separates stubbing from

verification

Page 42: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Classic mocking doesn’t separate

stubbing from verification

JMock:

EasyMock:

Page 43: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mockito knows developers read stack trace

Page 44: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mockito knows developers read stack trace

Page 45: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mockito is a Test Spy framework

run()

verifyThis()

verifyThat()

Spying

run()

assertThis()

assertThat()

expectThis()

expectThat()

run()

verify()

Classic testingClassic mocking

Page 46: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Mockito and classic testing are explicit

strict style requires explicit specification:

assertNotTrue(something);verify(mock, never()).method();

loose style requires explicit specification:

ignoreInteractions(mock);

loose by defaultstrict by default

Classic testing and MockitoClassic mocking

Page 47: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

The current era in my project is Mockitozoic!

� jMockozoic ->

� EasyMockozoic ->

� HandMockozoic ->

� Mockitozoic

Page 48: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

What’s next?

� jMockozoic ->

� EasyMockozoic ->

� HandMockozoic ->

� Mockitozoic ->

� ?

Page 49: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

What I don’t like about Mockito

� a bit inconsistent API:� verify(mock).method();

� stub(mock.method()).toReturn(x);

� stubbing voids is different:� doThrow(ex).when(mock).method();

� may lead to overmocking because it’s too easy to mock ☺

Page 50: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

What users like about Mockito?

� explicit API

� flexible verification

� separation of stubbing and verification

� @Mock annotation

� expectations after exercising

Page 51: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

What are the plans for Mockito:

� maintain slip API to promote simple code

� change the stubbing api:� instead: stub(mock.getStuff()).toReturn(x);

� do: when(mock.getStuff()).thenReturn(x);

� spread to other languages (python, c++, C#)

Page 52: don't give up on mocking - · PDF fileDon’t give up on mocking-Why do people give up?-Mocking: the big step from classic way of testing-Let’s take a step back and don’t give

Regards

� jMock guys for inventing mock objects

� EasyMock guys for their innovative syntax

� Gerard Maszeros for sorting out mocking terminology

� Mockito users and contributors for their ideas