EasyMock Adding Behavior - Easy Mock

How to add functionality to a mock object in EasyMock?

A functionality is added to a mock object by EasyMock using the expect()and expectLassCall() methods by using the following code:

By this code, EasyMock is instructed to give a behaviour of adding 10 and 20 to the add method of calcService and as a result, to return the value of 30.00.

The behaviour is recorded by the Mock but does not work as a mock object. It starts working when it is called to replay.

Example without EasyMock.Replay()

Step 1: Create an interface called CalculatorService to provide mathematical functions

File: CalculatorService.java

Step 2: Create a JAVA class to represent MathApplication

File: MathApplication.java

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.

File: MathApplicationTester.java

Step 4: Execute test cases

Create a java class file named TestRunner in C:\>EasyMock_WORKSPACEto execute the test case(s).

File: TestRunner.java

Step 5: Verify the Result

Compile the classes using javac compiler as follows:

The Test Runner is run to observe the result.

The output is verified.

Example with EasyMock.Replay()

Step 1: Create an interface called CalculatorService to provide mathematical functions.

File: CalculatorService.java

Step 2: Create a JAVA class to represent MathApplication.

File: MathApplication.java

Step 3: Test the MathApplication class

The MathApplication class is tested by injecting a mock of calculatorService into it. EasyMock creates a Mock.

File: MathApplicationTester.java

Step 4: Execute test cases

Create a java class file named TestRunner in C:\>EasyMock_WORKSPACEto execute Test case(s).

File: TestRunner.java

Step 5: Verify the Result

Compile the classes using javac compiler as follows:

The Test Runner is run to observe the result

The output is verified.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Easy Mock Topics