Post on 20-Feb-2017
transcript
Mockito vs JMockit, battle of the mocking frameworks
The art of pretending
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
Mockito in action
Mockito in action
Mockito in action
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
This gun’ be good
This gonna be good
• Mocking void methods• Mocking finals• Mocking statics• Verifying constructors
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!
JMockit
JMockit in action
JMockit in action
JMockit in action
JMockit in action
• Expectations are like Mockito.when(…)
• Verifications are like Mockito.verify(…)
JMockit in action
• Uniform notation for• Primitives• Complex objects• Exceptions• Custom Answers
JMockit in action
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
… comes great responsibility - Scoping
Legacy mock injection
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)
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 …
Real world examples
JMockit
JMockit
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
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
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
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
“I Don’t Always Test My Code. But When I Do, I Do It In Production.”