পৃষ্ঠাসমূহ
Labels
Search Your Article
CS
Pageviews
Showing posts with label EasyMock Basics Tutorial. Show all posts
Showing posts with label EasyMock Basics Tutorial. Show all posts
Monday, January 16, 2017
EasyMock - Environment Setup
EasyMock is a framework for Java, so the very first requirement is to have JDK installed in your machine.
EasyMock - First Application
Before going into the details of the EasyMock Framework, let’s see an
application in action. In this example, we've created a mock of Stock
Service to get the dummy price of some stocks and unit tested a java
class named Portfolio.
EasyMock - JUnit Integration
In this chapter, we'll learn how to integrate JUnit and EasyMock
together. Here we will create a Math Application which uses
CalculatorService to perform basic mathematical operations such as
addition, subtraction, multiply, and division.
EasyMock - Adding Behavior
EasyMock adds a functionality to a mock object using the methods expect() and expectLassCall(). Take a look at the following code snippet.
//add the behavior of calc service to add two numbers EasyMock.expect(calcService.add(10.0,20.0)).andReturn(30.00);
EasyMock - Verifying Behavior
EasyMock can ensure whether a mock is being used or not. It is done using the verify() method. Take a look at the following code snippet.
//activate the mock EasyMock.replay(calcService);
EasyMock - Expecting Calls
EasyMock provides a special check on the number of calls that can be
made on a particular method. Suppose MathApplication should call the
CalculatorService.serviceUsed() method only once, then it should not be
able to call CalculatorService.serviceUsed() more than once.
EasyMock - Varying Calls
EasyMock provides the following additional methods to vary the expected call counts.
- times (int min, int max) – expects between min and max calls.
- atLeastOnce () – expects at least one call.
- anyTimes () – expects an unrestricted number of calls.
EasyMock - Exception Handling
EasyMock provides the capability to a mock to throw exceptions, so
exception handling can be tested. Take a look at the following code
snippet.
//add the behavior to throw exception
EasyMock - createMock
So far, we've used annotations to create mocks. EasyMock provides
various methods to create mock objects. EasyMock.createMock() creates
mocks without bothering about the order of method calls that the mock is
going to make in due course of its action.
EasyMock - createStrictMock
EasyMock.createStrictMock() creates a mock and also takes care of the
order of method calls that the mock is going to make in due course of
its action.
File: CalculatorService.java
File: MathApplication.java
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.createStrictMock(), the order of execution of the method does matter.
File: MathApplicationTester.java
Create a java class file named TestRunner in C:\> EasyMock_WORKSPACE to execute Test case(s).
File: TestRunner.java
Syntax
calcService = EasyMock.createStrictMock(CalculatorService.class);
Example
Step 1: Create an interface called CalculatorService to provide mathematical functionsFile: 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); }Step 2: Create a JAVA class 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); } }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.createStrictMock(), the order of execution of the method does matter.
File: MathApplicationTester.java
import org.easymock.EasyMock; import org.easymock.EasyMockRunner; import org.junit.Assert; import org.junit.Before; import 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.createStrictMock(CalculatorService.class); 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); } }Step 4: Execute test cases
Create a java class file named TestRunner in C:\> EasyMock_WORKSPACE to execute Test case(s).
File: TestRunner.java
EasyMock - createNiceMock
EasyMock.createNiceMock() creates a mock and sets the default
implementation of each method of the mock. If EasyMock.createMock() is
used, then invoking the mock method throws assertion error.
EasyMock - EasyMockSupport
EasyMockSupport is a utility or helper class for test classes. It provides the following functionalities:
- replayAll() – Registers all the created mocks in one batch.
- verifyAll() – Verifies all the mock operations in one batch.
- resetAll() – Resets all the mock operations in one batch.
EasyMock - Quick Guide
EasyMock - Overview
What is Mocking?
Mocking is a way to test the functionality of a class in isolation. Mocking does not require a database connection or properties file read or file server read to test a functionality. Mock objects do the mocking of the real service. A mock object returns a dummy data corresponding to some dummy input passed to it.EasyMock
EasyMock - Useful Resources
The following resources contain additional information on EasyMock.
Please use them to get more in-depth knowledge on this topic.
Discuss EasyMock
EasyMock is a mocking framework, JAVA-based library that is used for
effective unit testing of JAVA applications. EasyMock is used to mock
interfaces so that a dummy functionality can be added to a mock
interface that can be used in unit testing.
Subscribe to:
Posts (Atom)