关闭

MOCK编写UT Case的步骤

标签: objectparametersreturningjavadocejbjunit
3088人阅读 评论(0) 收藏 举报
分类:

使用MockObjectUT Case的步骤如下:

 

1、   新建一个JUnitTestCase类,父类指定为:com.kddi.smc.togo.commons.dbunit.MockSmcTestCase

2、   找出要测试的方法都依赖于哪些外部EJB对象。

3、   使用EasyMock类库辅助生成所依赖的外部对象的Mock实现:

·         使用MockControlcreateControl(Class)方法为每个依赖的接口生成MockControl对象

·         调用MockControlgetMock()方法获得所依赖的接口的mock实现

·         根据该TestCase要测试的内容,模拟调用mock对象里会被调用的方法,并设置方法的返回值

·         如果模拟的是EJBHome/LocalHome接口,那么需要调用父类中的bind(String, Object)方法将其绑定到模拟的JNDI环境中

4、   调用被测试的方法。

5、   分别调用每个MockControlEasyMock提供)verify方法来验证实际执行时是否与期望的相符。

 

 

Resouses:

 

EasyMock的官方网站http://www.easymock.org/

UT时大部分的情况下只会用到一个类:MockControl,该类中经常使用到的方法的Javadoc如下,更详细的信息请参考在线文档http://www.easymock.org/EasyMock1_2_Java1_3_Documentation.html

 

public static MockControl createControl(java.lang.Class toMock)

Creates a mock control object for the specified interface. The MockControl and its associated mock object will not check the order of expected method calls. An unexpected method call on the mock object will lead to an AssertionFailedError.

Parameters:

toMock - the class of the interface to mock.

Returns:

the mock control.

 

public java.lang.Object getMock()

Returns the mock object.

Returns:

the mock object of this control

 

public void replay()

Switches the mock object from record state to replay state.

Throws:

java.lang.IllegalStateException - if the mock object already is in replay state

 

public void verify()

Verifies that all expectations have been met.

Throws:

java.lang.IllegalStateException - if the mock object is in record state.

junit.framework.AssertionFailedError - if any expectation has not been met

 

public void setDefaultReturnValue(java.lang.Object value)

Records that the mock object will by default allow the last method specified by a method call, and will react by returning the provided return value.

Parameters:

value - the return value.

Throws:

java.lang.IllegalStateException - if the mock object is in replay state, if no method was called on the mock object before. or if the last method called on the mock does not return an object.

java.lang.IllegalArgumentException - if the provided return value is not compatible to the return value of the last method called on the mock object.

 

public void setReturnValue(java.lang.Object value)

Records that the mock object will expect the last method call once, and will react by returning the provided return value.

Parameters:

value - the return value.

Throws:

java.lang.IllegalStateException - if the mock object is in replay state, if no method was called on the mock object before. or if the last method called on the mock does not return an object.

java.lang.IllegalArgumentException - if the provided return value is not compatible to the return value of the last method called on the mock object.

 

public void setThrowable(java.lang.Throwable throwable)

Records that the mock object will expect the last method call once, and will react by throwing the provided Throwable.

Parameters:

throwable - the Throwable to throw.

Throws:

java.lang.IllegalStateException - if the mock object is in replay state or if no method was called on the mock object before.

java.lang.IllegalArgumentException - if the last method called on the mock cannot throw the provided Throwable.

java.lang.NullPointerException - if throwable is null.

 

public void setDefaultThrowable(java.lang.Throwable throwable)

Records that the mock object will by default allow the last method specified by a method call, and will react by throwing the provided Throwable.

Parameters:

throwable - throwable the throwable to be thrown

Throws:

java.lang.IllegalArgumentException - if the last method called on the mock cannot throw the provided Throwable.

java.lang.NullPointerException - if throwable is null.

java.lang.IllegalStateException - if the mock object is in replay state, or if no method was called on the mock object before.

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:251310次
    • 积分:2886
    • 等级:
    • 排名:第12242名
    • 原创:44篇
    • 转载:66篇
    • 译文:1篇
    • 评论:31条
    最新评论