这个教程基于JUnit介绍使用EasyMock框架做测试。
EasyMock介绍
EasyMock基于接口或者类实例化对象:
import static org.easymock.EasyMock.createNiceMock;....// ICalcMethod is the object which is mockedICalcMethod calcMethod = createNiceMock(ICalcMethod.class);
createNiceMock()
方法创建的mock返回默认值而不是重载方法。Mock()则不能这样创建
。
EasyMock的有几种方法用于配置mock对象。expect()
方法告诉EasyMock在给定参数的情况下模拟方法,andReturn()
对应的返回值。times()
方法定义调用次数。replay()
方法被调用后模拟对象就可用了。
// setup the mock objectexpect(calcMethod.calc(Position.BOSS)).andReturn(70000.0).times(2);expect(calcMethod.calc(Position.PROGRAMMER)).andReturn(50000.0);// Setup is finished need to activate the mockreplay(calcMethod);
EasyMock的最新版本(2015年底 3.4) http://easymock.org/下载,旧版下载参见:http://sourceforge.net/projects/easymock/files/EasyMock/。定义如下java类:
package com.vogella.testing.easymock.first;public enum Position { BOSS, PROGRAMMER, SURFER}
package com.vogella.testing.easymock.first;public interface ICalcMethod { double calc(Position position);}
package com.vogella.testing.easymock.first;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); }}
要测试的类是IncomeCalculator。 这个类用来计算指定职位和计算方法的薪水。
测试类IncomeCalculator如下:
package com.oppo.ads.searchBid.task.thread;import org.junit.Test;import static org.junit.Assert.*;// use static imports to// have direct access to these methodsimport 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;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); 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 testCalc2() { // Setting up the expected value of the method call calc expect(calcMethod.calc(Position.SURFER)).andThrow(new RuntimeException("Don't know this guy")).times(1); // Setup is finished need to activate the mock replay(calcMethod); calc.setPosition(Position.SURFER); calc.setCalcMethod(calcMethod); calc.calc(); }}
可见EasyMock需要基于接口来测试,操作起来还是比较麻烦的!
参考资料
http://www.vogella.com/tutorials/EasyMock/article.html