MOCK编写UT Case的步骤

来源:互联网 发布:时空医药软件好用吗 编辑:程序博客网 时间:2024/05/16 04:55

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

原创粉丝点击