Easy mock

62 %
38 %
Information about Easy mock

Published on September 8, 2016

Author: prathibhak2

Source: slideshare.net

1. EASY MOCK EasyMock is a framework for creating mock objects using the java.lang.reflect.Proxy object. When a mock object is created, a proxy object takes the place of the real object. The proxy object gets its definition from the interface or class you pass when creating the mock.

2. Mock Objects • Unit testing is the testing of a component in isolation. However,in most systems objects have many dependencies. In order to be able to test code in isolation, those dependencies need to be removed to prevent any impact on test code by the dependant code. To create this isolation, mock objects are used to replace the real objects. • EasyMock has two sets of APIs. One is intended for creation and manipulation of mock objects that are based on interfaces, the other on classes (org.easymock.EasyMock and org.easymock. classextensions.EasyMock respectively). Both provide the same basic functionality; however classextensions does not have quite as extensive as an API as the regular EasyMock does.

3. Creating Objects with EasyMock • There are two main ways to create a mock object using EasyMock, directly and thru a mock control. When created directly, mock objects have no relationship to each other and the validation of calls is independent. When created from a control,all of the mock objects are related to each other. This allows for validation of method calls across mock objects (when created with the EasyMock.createStrictControl() method).

4. Installation • EasyMock is a framework for Java, so the very first requirement is to have JDK installed in your machine. • Using Maven • EasyMock is available in the Maven central repository. Just add the following dependency to your pom.xml: • Download the EasyMock zip file • It contains the easymock-3.4.jar to add to your classpath • To perform class mocking, also add to your classpath. • The bundle also contains jars for the javadoc, the tests, the sources and the samples

5. EasyMock Mock Object Lifecycle • EasyMock has a lifecycle similar to JUnit. It contains four stages. createmock expect verify replay

6. • Create mock: This phase creates the mock object. • Expect: This phase records the expected behaviors of the mock object. These will be verified at the end. • Replay: Replays the previously recorded expectations. • Verify:In order for a test to pass, the expected behaviors must have been executed. The verify phase confirms the execution of the expected calls.

7. Objects in Easymock • Regular/td:A test fails if a method is called that is not expected or if a method that is expected is not called. Order of method calls does not matter. • Nice:A test fails if a method is expected but not called. Methods that are called but are not expected are returned with a type appropriate default value (0, null or false). Order of method calls does not matter. • Strict:A test fails if a method is called that is not expected or if a method that is expected is not called. Order of method calls does matter.

8. Recording Behavior in EasyMock • There are three groups of scenarios that exist when recording behavior: void methods, non void methods and methods that throw exceptions. Each of which is handled slightly different. • Void Methods • Void methods are the easiest behavior to record. Since they do not return anything, all that is required is to tell the mock object what method is going to be called and with what parameters. This is done by calling the method just as you normally would. • Code Being Tested • … foo.bar(); String string = “Parameter 2”; foo.barWithParameters(false, string); … • Mocking the Behavior • Foo fooMock = EasyMock.createMock(Foo.class); fooMock.bar(); fooMock.barWithParameters(false, “Parameter 2”); …

9. Methods That Return Values • When methods return values a mock object needs to be told the method call and parameters passed as well as what to return. The method EasyMock.expect() is used to tell a mock object to expect a method call. • Code to Be Tested • … String results = foo.bar(); String string = “Parameter 2”; BarWithParametersResults bwpr = foo. barWithParameters(false, string); … Mocking the Behavior • … Foo fooMock = EasyMock.createMock(Foo.class); EasyMock.expect(foo.bar()).andReturn(“results”); EasyMock.expect(foo.barWithParameters(false, “Parameter 2”)) .andReturn(new BarWithParametersResults()); …

10. Methods That Throw Exceptions • Negative testing is an important part of unit testing. In order to be able to test that a method throws the appropriate exceptions when required, a mock object must be able to throw an exception when called. • Repeated Calls • There are times where a method will be called multiple times or even an unknown number of times. EasyMock provides the ability to indicate those scenarios with the • .times(), .atleastOnce() and .anyTimes() methods. … Foo fooMock = EasyMock.createMock(Foo.class); EasyMock.expect(foo.bar()).andReturn(“results”). anyTimes(); EasyMock.expect(foo.barWithParameters(false, “Parameter 2”)) .andReturn(new BarWithParametersResults()). atLeastOnce(); …

11. Step 1: Create an interface called CalculatorService to provide mathematical functions File: CalculatorService.java • public interface CalculatorService { • public double add(double input1, double input2); • public double subtract(double input1, double input2); • public double multiply(double input1, double input2); • public double divide(double input1, double input2); • }

12. Step 2: Create a JAVAclass to represent MathApplication File: MathApplication.java • public class MathApplication { • private CalculatorService calcService; • public void setCalculatorService(CalculatorService calcService) • { • this.calcService = calcService; • } public double add(double input1, double input2) • { • return calcService.add(input1, input2); • } • public double subtract(double input1, double input2){ • return calcService.subtract(input1, input2); • } public double multiply(double input1, double input2){ • return calcService.multiply(input1, input2); • } public double divide(double input1, double input2){ • return calcService.divide(input1, input2); • } • }

13. Step 3: Test the MathApplication class • Let's test the MathApplication class, by injecting in it a mock of calculatorService. Mock will be created by EasyMock. • Here we've added two mock method calls, add() and subtract(), to the mock object via expect(). However during testing, we've called subtract() before calling add(). When we create a mock object using EasyMock.createMock(), the order of execution of the method does not matter. • File: MathApplicationTester.java

14. • import org.easymock.EasyMock; • import org.easymock.EasyMockRunner; I • mport org.junit.Assert; • import org.junit.Before; I • mport org.junit.Test; • import org.junit.runner.RunWith; • @RunWith(EasyMockRunner.class) • public class MathApplicationTester { • private MathApplication mathApplication; • private CalculatorService calcService; • @Before public void setUp() • { mathApplication = new MathApplication(); • calcService = EasyMock.createMock(CalculatorService.class);

15. • mathApplication.setCalculatorService(calcService); • } • @Test public void testAddAndSubtract(){ • //add the behavior to add numbers EasyMock.expect(calcService.add(20.0,10.0)).andReturn(30.0); //subtract the behavior to subtract numbers EasyMock.expect(calcService.subtract(20.0,10.0)).andReturn(10.0); //activate the mock EasyMock.replay(calcService); //test the subtract functionality Assert.assertEquals(mathApplication.subtract(20.0, 10.0),10.0,0); //test the add functionality Assert.assertEquals(mathApplication.add(20.0, 10.0),30.0,0); //verify call to calcService is made or not EasyMock.verify(calcService); } }

16. TestRunner.java • import org.junit.runner.JUnitCore; • import org.junit.runner.Result; • import org.junit.runner.notification.Failure; • public class TestRunner { • public static void main(String[] args) { • Result result = JUnitCore.runClasses(MathApplicationTester.class); • • for (Failure failure : result.getFailures()) { • System.out.println(failure.toString()); • } • • System.out.println(result.wasSuccessful()); • } • }

17. Verify the Result • Compile the classes using javac compiler as follows: • C:EasyMock_WORKSPACE>javac MathApplicationTester.java • Now run the Test Runner to see the result: • C:EasyMock_WORKSPACE>java TestRunner • Verify the output. • true

Add a comment