MOCK编写UT Case的步骤
来源:互联网 发布:时空医药软件好用吗 编辑:程序博客网 时间:2024/05/16 04:55
使用MockObject写UT Case的步骤如下:
1、 新建一个JUnit的TestCase类,父类指定为:com.kddi.smc.togo.commons.dbunit.MockSmcTestCase
2、 找出要测试的方法都依赖于哪些外部EJB对象。
3、 使用EasyMock类库辅助生成所依赖的外部对象的Mock实现:
· 使用MockControl的createControl(Class)方法为每个依赖的接口生成MockControl对象
· 调用MockControl的getMock()方法获得所依赖的接口的mock实现
· 根据该TestCase要测试的内容,模拟调用mock对象里会被调用的方法,并设置方法的返回值
· 如果模拟的是EJB的Home/LocalHome接口,那么需要调用父类中的bind(String, Object)方法将其绑定到模拟的JNDI环境中
4、 调用被测试的方法。
5、 分别调用每个MockControl(EasyMock提供)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.
- MOCK编写UT Case的步骤
- MOCK编写UT Case的步骤
- Python mock UT
- mock.patch.object方式写UT
- [OpenStack UT] 分析OpenStack中单元测试之mock & mox
- 串口的编写步骤
- 编写代码的步骤
- 编写Activity的步骤
- ListView的编写步骤
- UT
- UT
- ut
- UT
- 利用Python Mock模拟OJ test case
- 接口测试webserver mock编写
- 接口测试tcp mock编写
- WSDL编写的五个步骤
- WSDL编写的五个步骤
- windows内核初窥(二)-----系统机制
- 使用批处理命令给客户更新oracle数据库
- 6月14日软件推荐
- OSWorkflow的第一个程序
- Splicing MPEG Video Streams in the Compressed Domain(翻译)
- MOCK编写UT Case的步骤
- ASP,checkbox多选删除
- 《C++沉思录》通过句柄来避免不必要的对象的复制
- 数据源的配置和使用
- 网页中的下拉菜单
- 32位图像处理库 delphi简单实现(转贴)
- TreeSet() 类逆向排序(实现compare()方法以便按正常顺序的逆向进行操作)。
- 网络名言 2006.6.14
- 社交网络、手机视频迅速崛起 将再造互联网