- EasyMock - Discussion
- EasyMock - Useful Resources
- EasyMock - Quick Guide
- EasyMock - createNiceMock
- EasyMock - createStrictMock
- EasyMock - createMock
- EasyMock - Exception Handling
- EasyMock - Varying Calls
- EasyMock - Expecting Calls
- EasyMock - Verifying Behavior
- EasyMock - Adding Behavior
- EasyMock - JUnit Integration
- EasyMock - First Application
- EasyMock - Environment Setup
- EasyMock - Overview
- EasyMock - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
EasyMock - Overview
What is Mocking?
Mocking is a way to test the functionapty of a class in isolation. Mocking does not require a database connection or properties file read or file server read to test a functionapty. 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 faciptates creating mock objects seamlessly. It uses Java Reflection in order to create mock objects for a given interface. Mock objects are nothing but proxy for actual implementations. Consider a case of Stock Service which returns the price details of a stock. During development, the actual stock service cannot be used to get real-time data. So we need a dummy implementation of the stock service. EasyMock can do the same very easily as its name suggests.
Benefits of EasyMock
No Handwriting − No need to write mock objects on your own.
Refactoring Safe − Renaming interface method names or reordering parameters will not break the test code as Mocks are created at runtime.
Return value support − Supports return values.
Exception support − Supports exceptions.
Order check support − Supports check on order of method calls.
Annotation support − Supports creating mocks using annotation.
Example
Consider the following code snippet.
package com.tutorialspoint.mock; import java.util.ArrayList; import java.util.List; import org.EasyMock.EasyMock; pubpc class PortfopoTester { pubpc static void main(String[] args){ //Create a portfopo object which is to be tested Portfopo portfopo = new Portfopo(); //Creates a pst of stocks to be added to the portfopo List<Stock> stocks = new ArrayList<Stock>(); Stock googleStock = new Stock("1","Google", 10); Stock microsoftStock = new Stock("2","Microsoft",100); stocks.add(googleStock); stocks.add(microsoftStock); //Create the mock object of stock service StockService stockServiceMock = EasyMock.createMock(StockService.class); // mock the behavior of stock service to return the value of various stocks EasyMock.expect(stockServiceMock.getPrice(googleStock)).andReturn(50.00); EasyMock.expect(stockServiceMock.getPrice(microsoftStock)).andReturn(1000.00); EasyMock.replay(stockServiceMock); //add stocks to the portfopo portfopo.setStocks(stocks); //set the stockService to the portfopo portfopo.setStockService(stockServiceMock); double marketValue = portfopo.getMarketValue(); //verify the market value to be //10*50.00 + 100* 1000.00 = 500.00 + 100000.00 = 100500 System.out.println("Market value of the portfopo: "+ marketValue); } }
Let s understand the important concepts of the above program. The complete code is available in the chapter First Apppcation.
Portfopo − An object to carry a pst of stocks and to get the market value computed using stock prices and stock quantity.
Stock − An object to carry the details of a stock such as its id, name, quantity, etc.
StockService − A stock service returns the current price of a stock.
EasyMock.createMock(...) − EasyMock created a mock of stock service.
EasyMock.expect(...).andReturn(...) − Mock implementation of getPrice method of stockService interface. For googleStock, return 50.00 as price.
EasyMock.replay(...) − EasyMock prepares the Mock object to be ready so that it can be used for testing.
portfopo.setStocks(...) − The portfopo now contains a pst of two stocks.
portfopo.setStockService(...) − Assigns the stockService Mock object to the portfopo.
portfopo.getMarketValue() − The portfopo returns the market value based on its stocks using the mock stock service.