MOCK编写UT Case的步骤

原创 2006年06月14日 10:20:00

使用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.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

QT基本数据类型

QT的基本数据类型qint8:signed char 有符号8比特数据 qint16:signed short 16位数据类型 qint32:signed int. 32位有符号数据类型 ...

使用Subversion构建版本控制环境(Windows)

v/:* {behavior:url(#default#VML);}o/:* {behavior:url(#default#VML);}w/:* {behavior:url(#default#VML)...

QT中数据类型转换总结

最近写Qt中的tcp网络编程,Socke连接后,接受到的数据类型是字节型,这就涉及到了大量的类型转换,在网上辗转几辄,总算有了点结果,特此跟大家分享。好了,不废话,下面细说。   1.打开Qt C...

QT 知识点汇总(二)

http://no001.blog.51cto.com/1142339/262389 1.程序可以显示中文 #include QTextCodec::setCodecForTr(QTe...

QT中的数据类型

QT中的数据类型--转自 - Global Qt Declarations   http://doc.qt.nokia.com/latest/qtglobal...

QT 常用部件 (四)自动完成LineEdit

http://mobile.51cto.com/symbian-271180.htm Qt 自动完成LineEdit是本文要介绍的内容,内容虽少,取其精华。简单的代码实现出很好的效果,先来看...

RT_thread 学习笔记2——TCP/IP协议栈(LwIP)

netconn write() 原型声明  int  netconn write(struct  netconn *conn,  void *data, int  len,  unsigned in...

LWIP互联网资料汇总

本文主要搜集了下互联网上关于LWIP的资料和教程 欢迎补充 第一部分:移植 LWIP在UCOS上移植 LWIP 在STM32上移植   http://www.docin.com/p-4...

嵌入式cgi开发 很好的一篇文章

http://blog.ednchina.com/longhaihai/255858/Message.aspx#
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)