পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Monday, January 16, 2017

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 facilitates 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.
Consider the following code snippet.
package com.tutorialspoint.mock;

import java.util.ArrayList;
import java.util.List;

import org.EasyMock.EasyMock;

public class PortfolioTester {
   public static void main(String[] args){

      //Create a portfolio object which is to be tested  
      Portfolio portfolio = new Portfolio();

      //Creates a list of stocks to be added to the portfolio
      List stocks = new ArrayList();
      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 portfolio
      portfolio.setStocks(stocks);

      //set the stockService to the portfolio
      portfolio.setStockService(stockServiceMock);

      double marketValue = portfolio.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 portfolio: "+ marketValue);
   }
}
Let's understand the important concepts of the above program. The complete code is available in the chapter First Application.
  • Portfolio – An object to carry a list 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.
  • portfolio.setStocks(...) – The portfolio now contains a list of two stocks.
  • portfolio.setStockService(...) - Assigns the stockService Mock object to the portfolio.
  • portfolio.getMarketValue() – The portfolio returns the market value based on its stocks using the mock stock service.

No comments:

Post a Comment