首页 > 其他 > 详细

EasyMock测试教程

时间:2015-11-12 19:40:59      阅读:256      评论:0      收藏:0      [点我收藏+]

这个教程基于JUnit介绍使用EasyMock框架做测试。

EasyMock介绍

EasyMock基于接口或者类实例化对象:

import static org.easymock.EasyMock.createNiceMock;
....

// ICalcMethod is the object which is mocked
ICalcMethod calcMethod = createNiceMock(ICalcMethod.class); 

createNiceMock()方法创建的mock返回默认值而不是重载方法。Mock()则不能这样创建

EasyMock的有几种方法用于配置mock对象。expect()方法告诉EasyMock在给定参数的情况下模拟方法,andReturn()对应的返回值。times()方法定义调用次数。replay()方法被调用后模拟对象就可用了。

// setup the mock object
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); 

 

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 methods
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;


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

 

 

EasyMock测试教程

原文:http://www.cnblogs.com/pythontesting/p/4959801.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!