Easymock Tutorial

57,586 views

Published on

EasyMock tutorial
Tutorial to cover types of mocking,how to mock
advantages disadvanatages

Published in: Technology, Education
1 Comment
23 Likes
Statistics
Notes
No Downloads
Views
Total views
57,586
On SlideShare
0
From Embeds
0
Number of Embeds
1,762
Actions
Shares
0
Downloads
1,382
Comments
1
Likes
23
Embeds 0
No embeds

No notes for slide

Easymock Tutorial

  1. 1. Unit testing with mock objects-1 16/7/2009
  2. 2. Agenda <ul><li>What is mocking? </li></ul><ul><li>Why to use mocking? </li></ul><ul><li>Types of mocking </li></ul><ul><li>Easymock </li></ul><ul><li>Simple Examples of EasyMock(with extensions) and PowerMock </li></ul>
  3. 3. What is mocking? <ul><li>Mocking allows you to test a class or method in isolation . </li></ul><ul><li>A class/method maybe dependent on many other classes/methods. It is with the help of others( collaborators ) a class/method completes itz functionality. </li></ul><ul><li>In mocking we replace all of its collaborators with mocks that essentially simulate the normal environment of the class/method. </li></ul><ul><li>In other words: “ A mock object is a dummy interface or class in which you define the dummy output of a certain method call.” </li></ul>
  4. 4. Why to use mocking? <ul><li>In mocking we replace all of its collaborators with mocks </li></ul><ul><li>DATABASE is not required so NO dbunit </li></ul><ul><li>Use of Context is avoided </li></ul><ul><li>Dependencies can be ignored </li></ul><ul><li>We just test the core functinality of the class/method and we dont have to test the collaborators. </li></ul><ul><li>Easy, </li></ul><ul><li>less time to write junit for complex legacy code </li></ul>
  5. 5. How Mock Objects Work? <ul><li>2 Types in general </li></ul><ul><li>Proxy based like easymock,jmock </li></ul><ul><li>remap the class file in the class loader like jmockit ,powermock </li></ul>
  6. 6. 1. Proxy based <ul><li>Reflection : Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime , without knowing the names of the classes, methods etc. at compile time. </li></ul><ul><li>It is also possible to instantiate new objects, invoke methods and get/set field values using reflection. </li></ul><ul><li>Using Java Reflection you create dynamic implementations of interfaces at runtime. You do so using the class java.lang.reflect.Proxy </li></ul><ul><li>you can set what return values the proxy must return. </li></ul><ul><li>Proxy is a special class that allows intercept a set of methods identified by an interface public Object invoke(Object proxy, Method method, Object[] args) </li></ul>
  7. 7. 1. Proxy based... <ul><li>a proxy object is used to imitate the real object your code is dependent on. </li></ul>
  8. 8. 2. Remap the class file <ul><li>you tell the class loader to remap the reference to the class file it will load. </li></ul><ul><li>So let's say that I have a class Employeee with the corresponding .class file called Employeee .class and I want to mock it to use MyMock instead. </li></ul><ul><li>By using this type of mock objects, you will actually remap in the classloader the reference from Employeee to MyMock .class. </li></ul><ul><li>Uses the Instrumentation API. </li></ul>
  9. 9. What is EasyMock ? <ul><li>http://easymock.org/ </li></ul><ul><li>easymock framework helps you create mock objects. </li></ul><ul><li>Others are : jmock / Mockito / rMock / jMockit / sevenMock </li></ul>
  10. 10. How to use easymock? <ul><li>// Create a mock </li></ul><ul><li>SomeInterface mock = createMock ( SomeInterface.class ); </li></ul><ul><li>// Record behavior </li></ul><ul><li>expect ( mock.doStuff( &quot;argument&quot; )).andReturn( &quot;returnValue&quot; ); </li></ul><ul><li>// Replay behaviour </li></ul><ul><li>replay ( mock ); </li></ul><ul><li>// Executing the code we want to test </li></ul><ul><li>String newValue=test.perform() </li></ul><ul><li>assume perform() method calls doStuff() method. </li></ul><ul><li>// Verify behaviour </li></ul><ul><li>verify (mock); </li></ul>
  11. 11. Step 1 : Create a Mock <ul><li>Identify the classes/methods which are the collaborators.We can mock the classes by first mockin the Interface </li></ul><ul><li>createMock method is used to create the mock object. </li></ul><ul><li>stmtMock = createMock ( Statement. class ) ; </li></ul>
  12. 12. Step 2 : Mock the methods <ul><li>In EasyMock we use a record / replay approach. </li></ul><ul><li>record means -->You first train the mock by making the expected method calls on it . </li></ul><ul><li>replay means -->it tells EasyMock to stop recording . After that, it calls on the object to return the set values. If it gets a call it does not expect, it throws an Exception to fail fast. </li></ul><ul><li>It does not actually replay the mock. BUT it resets the mock so that the next time its methods are called it will begin replaying. </li></ul><ul><li>st.executeQuery(&quot;SELECT * FROM survey&quot;); </li></ul><ul><li>expect (stmtMock.executeQuery(&quot;SELECT * FROM survey&quot;)) </li></ul><ul><ul><li>. andReturn (rsMock); </li></ul></ul>
  13. 13. Step 3 : Execute the code to test <ul><li>AssertEquals ( 55 , idao .getNoOfColumns() ); </li></ul>
  14. 14. Step 4 : Verify the behaviour <ul><li>verify means-->verify( ) method checks to see if the mock actually received all the calls you expect </li></ul><ul><li>The method is used to make sure that all of the behaviour expected from the collaborators is valid. </li></ul><ul><li>verify ( stmtMock ); </li></ul>
  15. 15. pom.xml <ul><li><dependency> </li></ul><ul><li><groupId>org.easymock</groupId> </li></ul><ul><li><artifactId>easymockclassextension</artifactId> </li></ul><ul><li><version>2.4</version> </li></ul><ul><li><scope>test</scope> </li></ul><ul><li></dependency> </li></ul><ul><li><dependency> </li></ul><ul><li><groupId>org.easymock</groupId> </li></ul><ul><li><artifactId>easymock</artifactId> </li></ul><ul><li><version>2.4</version> </li></ul><ul><li><scope>test</scope> </li></ul><ul><li></dependency> </li></ul>
  16. 16. import <ul><li>import static org.easymock.EasyMock.*; </li></ul><ul><li>import static org.easymock.classextension.EasyMock.*; </li></ul>
  17. 17. Syntax <ul><li>To Throw An Exception </li></ul><ul><li>expect(mock.method()). andThrow (expectedException); </li></ul><ul><li>Expecting A Method With void Return Type </li></ul><ul><li>mock.clear(); </li></ul><ul><li>ExpectLastCall (); </li></ul><ul><li>Need to mention no. Of calls </li></ul><ul><li>expectLastCall(). times (3); </li></ul>
  18. 18. Syntax... <ul><li>Argument Matchers </li></ul><ul><li>eq (X value) </li></ul><ul><li>isA (Class clazz) </li></ul><ul><li>anyBoolean(), anyByte(), anyChar(), anyDouble(), anyFloat(), anyInt(),anyLong(), anyObject(), anyShort() </li></ul>
  19. 19. Drawbacks of easymock <ul><li>Cannot mock static methods </li></ul><ul><li>Cannot mock private methods </li></ul><ul><li>Cannot mock final methods </li></ul><ul><li>Cannot mock concrete classes </li></ul><ul><li>Cannot mock Constructors </li></ul><ul><li>jMock does not force us to put expectations on things that are irrelevant to the test at hand. If we're testing Cache.add(), it does not matter what the underlying call to Map.put() returns. With EasyMock, we have to fully specify everything, whether it matters or not </li></ul><ul><li>Use easymock.extensions </li></ul>
  20. 20. Unit testing with mock objects-2
  21. 21. Agenda <ul><li>More about easymock </li></ul><ul><li>Demo </li></ul>
  22. 22. Types of objects in easymock <ul><li>Normal — createMock(): All of the expected methods must be called with the specified arguments. However, the order in which these methods are called does not matter . Calls to unexpected methods cause the test to fail. </li></ul><ul><li>Strict — createStrictMock(): All expected methods must be called with the expected arguments, in a specified order. Calls to unexpected methods cause the test to fail. </li></ul><ul><li>Nice — createNiceMock(): All expected methods must be called with the specified arguments in any order. Calls to unexpected methods do not cause the test to fail. Nice mocks supply reasonable defaults for methods you don't explicitly mock. Methods that return numbers return 0. Methods that return booleans return false. Methods that return objects return null. </li></ul>
  23. 23. Use of verify <ul><li>The verify phase confirms the execution of the expected calls. </li></ul><ul><li>Useful in case of strict Mocks,where you need to check whether you called the methods,in right order.It will show all missing method calls. </li></ul><ul><li>In case of regular mocks it is not necessary. </li></ul>
  24. 24. Repeated calls <ul><li>For every method invoked by the mock we need an expectation. </li></ul><ul><li>By default mock expects minimum 1 call. </li></ul><ul><li>times (int min, int max) : to expect between min and max calls, </li></ul><ul><li>AtLeastOnce () : to expect at least one call, and </li></ul><ul><li>AnyTimes () : to expected an unrestricted number of calls. </li></ul>
  25. 25. Matchers in EasyMock <ul><li>eq (X value) : Matches if the actual value is equals the expected value. Available for all primitive types and for objects. </li></ul><ul><li>IsNull () : Matches if the actual value is null. Available for objects. </li></ul><ul><li>NotNull () : Matches if the actual value is not null. Available for objects. </li></ul><ul><li>startsWith (String prefix), contains (String substring), endsWith (String suffix) : Matches if the actual value starts with/contains/ends with the given value. Available for Strings. </li></ul><ul><li>matches (String regex), find (String regex) : Matches if the actual value/a substring of the actual value matches the given regular expression. Available for Strings. </li></ul>

×