Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views
easymock-3.2.jar
File Size: 119 kb
File Type: jar
Download File

EasyMock using EasyMock2

Create a project & then create an interface having all the operation signatures of the 3rd party class which you will be calling in your own class. As shown below -

package com.nitin.checking;

public interface ICalcMethod {
    double calc(Position position);
}

Below is your class where you will be calling above operation in your logic -

package com.nitin.checking;

public class IncomeCalculator {

    private ICalcMethod calcMethod;
    private Position position;

    public void setCalcMethod(ICalcMethod calcMethod) {
        this.calcMethod = calcMethod;
    }

    public void setPosition(Position position) {
        this.position = position;
    }

    public double calc() {
        if (calcMethod == null) {
            throw new RuntimeException("CalcMethod not yet maintained");
        }
        if (position == null) {
            throw new RuntimeException("Position not yet maintained");
        }
        return calcMethod.calc(position);
    }
}

Now let us write the JUnit class to test our above class -

package com.nitin.tests;

import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;
import com.nitin.checking.ICalcMethod;
import com.nitin.checking.IncomeCalculator;
import com.nitin.checking.Position;

public class IncomeCalculatorTest {

  private ICalcMethod calcMethod;
  private IncomeCalculator calc;

  @Before
  public void setUp() throws Exception {
    // NiceMocks return default values for
    // unimplemented methods
    calcMethod = createNiceMock(ICalcMethod.class);
    calc = new IncomeCalculator();
  }

  @Test
  public void testCalc1() {
    // Setting up the expected value of the method call calc
    expect(calcMethod.calc(Position.BOSS)).andReturn(70000.0).times(2);
    expect(calcMethod.calc(Position.PROGRAMMER)).andReturn(50000.0);
    // Setup is finished need to activate the mock
    replay(calcMethod);

    calc.setCalcMethod(calcMethod);
    try {
      calc.calc();
      fail("Exception did not occur");
    } catch (RuntimeException e) {

    }
    calc.setPosition(Position.BOSS);
    assertEquals(70000.0, calc.calc(), 0);
    assertEquals(70000.0, calc.calc(), 0);
    calc.setPosition(Position.PROGRAMMER);
    assertEquals(50000.0, calc.calc(), 0);
    calc.setPosition(Position.SURFER);
    verify(calcMethod);
  }

  @Test(expected = RuntimeException.class)
  public void testNoCalc() {
    calc.setPosition(Position.SURFER);
    calc.calc();
  }
}

Above code is from http://www.vogella.com/tutorials/EasyMock/article.html

Now let us look using EasyMock3.2

Below is the interface for the 3rd party class -

package com.nitin.checking;

public interface IEmployee {
 String getDesignation(String name);
}


Below is our class using 3rd party operation -
package com.nitin.checking;

public class Designations {
 
 private IEmployee employee;
 
 public void setIEmployee(IEmployee employee) {
  this.employee = employee;
 }

 public double getSalary(String name) throws Exception{
  if(name == null){
   return 0.0;
  }
  if(employee == null){
   throw new Exception("Employee not intialized");
  }
  if(employee.getDesignation(name).equalsIgnoreCase("Actor")){
   return 1000000.0;
  }
  return 0.0;
 }
}

Below is our JUnit class to test our above class -

package com.nitin.tests;

import org.junit.Test;
import org.junit.runner.RunWith;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import org.easymock.EasyMockRunner;
import org.easymock.TestSubject;
import org.easymock.Mock;
import com.nitin.checking.Designations;
import com.nitin.checking.IEmployee;

@RunWith(EasyMockRunner.class)
public class DesignationTest {

 @TestSubject
 Designations designation = new Designations(); // step 2

 @Mock
 private IEmployee employee; // step 1

 /*
  * The employee is instantiated by the runner at step 1. It is then set by
  * the runner, to the listener field on step 2. The setUp method can be
  * removed since all the initialization was done by the runner
  */

 @Test
 public void testGetSalary() throws Exception {
  expect(employee.getDesignation("John")).andReturn("Actor");
  replay(employee);
  // assertTrue(designation.getSalary("John") == 1000000.0);
  designation.getSalary("John");
  verify(employee);
 }
}

Attached is the jar file required to use EasyMock.

Important URL - http://easymock.org/EasyMock3_2_Documentation.html
                            http://java.dzone.com/articles/easymock-tutorial-%E2%80%93-getting



Powered by Create your own unique website with customizable templates.