Mockito vs JMockit, battle of the mocking frameworks
Transcript of Mockito vs JMockit, battle of the mocking frameworks
![Page 1: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/1.jpg)
Mockito vs JMockit, battle of the mocking frameworks
The art of pretending
![Page 2: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/2.jpg)
![Page 3: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/4.jpg)
Mockito in action
![Page 5: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/5.jpg)
Mockito in action
![Page 6: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/6.jpg)
Mockito in action
![Page 7: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/8.jpg)
This gun’ be good
![Page 9: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/9.jpg)
This gonna be good
• Mocking void methods• Mocking finals• Mocking statics• Verifying constructors
![Page 10: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/11.jpg)
JMockit
![Page 12: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/12.jpg)
JMockit in action
![Page 13: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/13.jpg)
JMockit in action
![Page 14: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/14.jpg)
JMockit in action
![Page 15: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/15.jpg)
JMockit in action
• Expectations are like Mockito.when(…)
• Verifications are like Mockito.verify(…)
![Page 16: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/16.jpg)
JMockit in action
• Uniform notation for• Primitives• Complex objects• Exceptions• Custom Answers
![Page 17: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/17.jpg)
JMockit in action
![Page 18: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/19.jpg)
… comes great responsibility - Scoping
![Page 20: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/20.jpg)
Legacy mock injection
![Page 21: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/23.jpg)
Real world examples
![Page 24: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/24.jpg)
JMockit
![Page 25: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/25.jpg)
JMockit
![Page 26: Mockito vs JMockit, battle of the mocking frameworks](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/28.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/29.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081419/58aa44ce1a28ab4c348b5e0d/html5/thumbnails/30.jpg)
“I Don’t Always Test My Code. But When I Do, I Do It In Production.”