easymock for java
DESCRIPTION
Mocking object based on EasyMock libraryTRANSCRIPT
![Page 1: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/1.jpg)
Learning art of Mocking by EasyMock
Deepak Singhvi
![Page 2: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/2.jpg)
2
Agenda
Goal Unit Testing… can be easy or hard What’s a Mock? Using Mock Objects Mock Frameworks To Mock or not to EasyMock Overview, Features, Drawbacks EasyMock in details – by examples Conclusion
![Page 3: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/3.jpg)
3
Goal of Presentation
Show how “Mocking Objects” can greatly improve unit testing by facilitating tests that are easier to write and less dependant upon objects outside the test domain
![Page 4: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/4.jpg)
4
UT is easy when…
Unit testing is easy when
– your code does not depend on any thing No database, file system, web service, socket, …
dependencies
– your code can be tested fully in isolation
–Does not require other classes/components, frameworks, etc.
You quickly create an object, run some tests, and off you go
![Page 5: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/5.jpg)
5
But remember UT means Automation
But when we talk about unit testing, we mean automated unit testing.
We need to be able to run tests quickly. Run them as many times as we like. You certainly don’t want to do either
laborious and/or manual setup and teardown. You want to check for behavior and ill
behavior of dependent code as well.
![Page 6: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/6.jpg)
6
testing is easy in isolation
Class Under Test
Test Class
![Page 7: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/7.jpg)
7
testing is harder with dependencies …
Class Under Test
Test Class
![Page 8: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/8.jpg)
8
… so remove the dependencies (for developer testing)
Class Under Test
Test Class
mock
mock
mockm
ock
![Page 9: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/9.jpg)
9
Topics of Discussion
What is Object Mocking? Why Mock Objects? What tools are available to support Object
Mockery
![Page 10: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/10.jpg)
10
What is Object Mocking?
Mock objects simulate (fake) real objects Allows for more modular testing Creating a mock implementation of an object that
can be used as a stand-in in testing Records and verifies expectations of calls A mock objects allow you to set expectations The mock object lets you verify that the expectations
were met or not Use the same Interface(s) as the real object
![Page 11: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/11.jpg)
11
Why engage in Object Mocking?
The real object has behavior that is hard to cause or is non-deterministic.
The real object is difficult to set up. The real object is slow. The real object has (or is ) a UI. Test needs to query the object but queries are not available. Real objects do not exist ( Team A is working on X and required Y.y
from Team B, at the same time team B is working on Y, to start the task of X team A can mock Y.y)
Can simulate both behavior and ill-behavior ( For e.g. there is a particular ill behavior which can appear only in the field, but we work on simulation and its difficult to get ill-behavior)
speeds up development of code under test
![Page 12: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/12.jpg)
12
To Mock or not to?
To– Mock only code that code under test directly depends on– Mock object that is hard to work with– Mock object that takes effort to set up– Mock object whose behavior is hard to predict– Mock object that requires lots of resources– Mock objects that are computationally expensive or very slow to respond– Mock objects that your test needs to verify with
Not To– Do not mock for the sake of mocking, ask if you can eliminate the need for mock by refactoring your code– Mock your objects but not resources like database, etc.– When writing the real code is easier than writing mock– Do not mock what will slow down your UT, mocks should be used to speed up UT
![Page 13: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/13.jpg)
13
What tools are available to support Obect Mockery
The MockObjects framework MockMaker JMock NMock EasyMock (We are interested only in this
particular tool)
![Page 14: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/14.jpg)
14
EasyMock Overview
Open source tool available at easymock.org Uses reflection to dynamically create proxies
that implement the interface at run time EasyMock generates two objects the
EasyMock Mock Object and the EasyMock Mock Control
![Page 15: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/15.jpg)
15
EasyMock Features
Allows the expected parameter value to be set for each method
Allows you to set up the value to be returned by each method
Allows the number of times the method is to be invoked to be specified
Allows you to specify an exception the a method should throw
Will verify that everything went as expected
![Page 16: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/16.jpg)
16
EasyMock Drawbacks
EasyMock generates objects on the fly so you can’t add any behaviour
EasyMock uses the equals method to compare parameters
![Page 17: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/17.jpg)
17
General
Homepage: www.easymock.org Lizence: MIT-Lizence
– Open Source– Very liberal
1. Version: 2003 Latest Version: April 2006
![Page 18: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/18.jpg)
18
Installation
Download easymock2.2.zip Add easymock.jar as library Used in Unit-test (JUnit/TestNG)
![Page 19: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/19.jpg)
19
EasyMock in detail
![Page 20: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/20.jpg)
20
How to use it
To get a Mock Object, we need to 1) Create a Mock Object for the interface we would like to simulate, 2) Record the expected behavior, 3) And switch the Mock Object to replay state.
EasyMock uses a record/replay metaphor for setting expectations. For each object you wish to mock you create a mock object. To indicate an expectation you call the method, with the arguments you expect on the mock. Once you've finished setting expectations you call replay - at which point the mock finishes the recording and is ready to respond to the primary object. Once done you call verify.
![Page 21: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/21.jpg)
21
Creating A Mock
IEnumeration<Short> iEnumIntfMock =
EasyMock.createMock(EnumeratedAttribute.class);
![Page 22: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/22.jpg)
22
Expecting A Lot
Expect method is called on mock Expect sequence of method calls on mock
(call createStrictMock()) Expect parameters to methods calls on mock
![Page 23: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/23.jpg)
23
Expect A Call
EasyMock.expect(iEnumIntfMock.getNumberOfRows());
![Page 24: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/24.jpg)
24
Expect A Return
EasyMock.expect(iEnumIntfMock.getNumberOfRows()).andReturn(5);
![Page 25: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/25.jpg)
25
Replay
Prime the pump to walk through expected call sequence
Provide return values in same sequence Listen to usage of call parameters
![Page 26: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/26.jpg)
26
Replay Kickoff
EasyMock.replay(iEnumIntfMock);
![Page 27: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/27.jpg)
27
Call all functions as you normally would Pass in mock where injectable Consider how mock can be passed into
internal references
![Page 28: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/28.jpg)
28
Testing
int numOfRows =
iEnumIntfMock.getNumberOfRows();
Assert.assertTrue("Number of rows does not match", numOfRows > 0);
![Page 29: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/29.jpg)
29
Verifying
![Page 30: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/30.jpg)
30
Called after all operations with the mock are done
Compares the usage to the recording
![Page 31: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/31.jpg)
31
Verifying
EasyMock.verify(iEnumIntfMock);
![Page 32: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/32.jpg)
32
Nice Mocks
Mocks created by EasyMock usually throw an AssertionError for all unexpected method calls. Sometimes you need a mock that nicely returns default values instead of complaining all the time. Mocks created by EasyMock.createNiceMock() return empty values (0, null, or false) for unexpected method calls.
![Page 33: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/33.jpg)
33
Strict Mocks
By default, the order of method calls is not checked by EasyMock. If you need this, you have to create a Strict Mock with EasyMock.createStrictMock(). You can even switch from strict to normal by EasyMock.checkOrder(Object mock, boolean enableOrderCheck).
![Page 34: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/34.jpg)
34
What about Mocking Classes with EasyMock?
So far we've used EasyMock to create mocks for interfaces, but how about mocking classes?
Is that possible? No. Yes. No. Yes, well, EasyMock itself is capable of mocking interfaces only.
But there is another project called EasyMock Class Extension that fills this gap. Just use the org.easymock.classextension.EasyMock.* methods instead. You
may even mock abstract classes. Unfortunately (but not surprisingly), you can't mock final classes.
![Page 35: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/35.jpg)
35
What this means
Object Mocking is an invaluable technique for improving the overall quality of your unit tests.
Help keep design decoupled Check code’s usage of another
object Test code from the inside out Make tests run faster Make it easier to write code for
problematic resources Defer having to write a class
Test components in isolation from the rest of the system
Promote interface-based design
Encourage composition over inheritance
Refine interfaces Test unusual, unlikely, and
exceptional situations
![Page 36: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/36.jpg)
36
Quiz Time
![Page 37: EasyMock for Java](https://reader033.vdocument.in/reader033/viewer/2022052504/554a4ca9b4c905863d8b5956/html5/thumbnails/37.jpg)
37
Mocking is one of the most popular tool used Agile and Extreme programming methodologies to follow the Test Driven Development
( popularly known as TDD).