30
Mockito vs JMockit, battle of the mocking frameworks The art of pretending

Mockito vs JMockit, battle of the mocking frameworks

Embed Size (px)

Citation preview

Page 1: Mockito vs JMockit, battle of the mocking frameworks

Mockito vs JMockit, battle of the mocking frameworks

The art of pretending

Page 2: Mockito vs JMockit, battle of the mocking frameworks
Page 3: Mockito vs JMockit, battle of the mocking frameworks

In all it’s glory

• Has been around since 2008• Is the industry standard ever since • In 2013 more then 30.000 Github

project• More popular then Spring and

Guava

• Most distinguishing feature• Verify the behaviour of the SUT

without establishing expectations beforehand

Page 4: Mockito vs JMockit, battle of the mocking frameworks

Mockito in action

Page 5: Mockito vs JMockit, battle of the mocking frameworks

Mockito in action

Page 6: Mockito vs JMockit, battle of the mocking frameworks

Mockito in action

Page 7: Mockito vs JMockit, battle of the mocking frameworks

So for, so good

• Arrange, Act, Assert• Mock any class, interface, method• No need for introducing test

interface shims• Declarative style for setting up

behaviour• Declarative style for verifying

behaviour

• …• In the happy flow

Page 8: Mockito vs JMockit, battle of the mocking frameworks

This gun’ be good

Page 9: Mockito vs JMockit, battle of the mocking frameworks

This gonna be good

• Mocking void methods• Mocking finals• Mocking statics• Verifying constructors

Page 10: Mockito vs JMockit, battle of the mocking frameworks

This gonna be good

• Make a test shim• Extend the class and scoop the

methods• Make an adapter or facade• Why aren’t they using interfaces

in the first place• The final keyword should be

banned• Let’s agree not to test this• Ahh.. let’s use

PowerMockito!

Page 11: Mockito vs JMockit, battle of the mocking frameworks

JMockit

Page 12: Mockito vs JMockit, battle of the mocking frameworks

JMockit in action

Page 13: Mockito vs JMockit, battle of the mocking frameworks

JMockit in action

Page 14: Mockito vs JMockit, battle of the mocking frameworks

JMockit in action

Page 15: Mockito vs JMockit, battle of the mocking frameworks

JMockit in action

• Expectations are like Mockito.when(…)

• Verifications are like Mockito.verify(…)

Page 16: Mockito vs JMockit, battle of the mocking frameworks

JMockit in action

• Uniform notation for• Primitives• Complex objects• Exceptions• Custom Answers

Page 17: Mockito vs JMockit, battle of the mocking frameworks

JMockit in action

Page 18: Mockito vs JMockit, battle of the mocking frameworks

Annotations - With great power…

• @Injectable• Single mock instance

• @Mocked• Entire class mock (at bytecode level)

• @Capturing• Any derived instance and subclass will be a class

mock

• @Tested• System under test• Tries to satisfy Constructor, @Autowired,

@Injectable

Page 19: Mockito vs JMockit, battle of the mocking frameworks

… comes great responsibility - Scoping

Page 20: Mockito vs JMockit, battle of the mocking frameworks

Legacy mock injection

Page 21: Mockito vs JMockit, battle of the mocking frameworks

Real world examples

java.lang.RuntimeException: Method getWindow in android.app.Activity not mocked. See http://g.co/androidstudio/not-mocked for details.

at android.app.Activity.getWindow(Activity.java) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:117) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:456) at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59) at ExampleActivity.onCreate(ExampleActivity.java:15) at ExampleActivityTest. shouldCommitFragment_whenOnCreateIsCalled(ExampleActivityTest.java:9)

Page 22: Mockito vs JMockit, battle of the mocking frameworks

Real world examplesakka.actor.ActorInitializationException: You cannot create an instance of [com.cc.LoggingHandler] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.

at akka.actor.ActorInitializationException$.apply(Actor.scala:173)at akka.actor.Actor$class.$init$(Actor.scala:436)at akka.actor.UntypedActor.<init>(UntypedActor.scala:97)at com.cc.LoggingHandler.<init>(LoggingHandler.java:14)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native

Method)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at …

Page 23: Mockito vs JMockit, battle of the mocking frameworks

Real world examples

Page 24: Mockito vs JMockit, battle of the mocking frameworks

JMockit

Page 25: Mockito vs JMockit, battle of the mocking frameworks

JMockit

Page 26: Mockito vs JMockit, battle of the mocking frameworks

JMockit’s Advantages

• Mockito’s distinguishing feature covered by JMockit• Verify the behaviour of the SUT without establishing

expectations beforehand

• No need for special test runners

• No limit on testing statics, finals, constructors and whatnot

• No limitations/anomalies in your code due to the test framework

Page 27: Mockito vs JMockit, battle of the mocking frameworks

JMockit’s Disadvantages

• Risk of introducing in your code• Static methods• Not using dependency injection

• Need to get accustomed to new mocking framework

• Mockito’s error messages are more explicit

• Mockito forces proper dependencies

Page 28: Mockito vs JMockit, battle of the mocking frameworks

Some figures

• Github grade and maven dependencies• Mockito: 131.525• JMockit: 3.058

• First mentioned - first release:• Mockito: 2008 - 2008• JMockit: 2006 - 2012

• Both under constant development

Page 29: Mockito vs JMockit, battle of the mocking frameworks

Read more

• Official pages:• Mockito: http://mockito.org/• JMockit: http://jmockit.org/

• My blog:• www.endran.nl/blog• Mockito vs JMockit, battle of the

mocking frameworks• Death to statics

Page 30: Mockito vs JMockit, battle of the mocking frameworks

“I Don’t Always Test My Code. But When I Do, I Do It In Production.”