easymock-3.2.jar | |
File Size: | 119 kb |
File Type: | jar |
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
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