最近在补充AT案例的时候,遇到异常的分支。本着程序员认真负责的态度,觉得应该也要把异常测试一下,于是上网搜索了一下
使用工具:jUnit4,主要使用了如下方法,
@Test
public void testHandleProcessEx()
{
ServiceRequest request = newServiceRequest();
List<CheckResultDTO> list =new ArrayList<CheckResultDTO>();
list.add(new CheckResultDTO());
list.add(new CheckResultDTO());
request.setParameter(CFSCheckNames.REJECT,list);
Throwable t = null;
try
{
enrollInputRejectHandler.handleProcess(request);
}
catch(Exception e)
{
t = e;
}
Assert.assertNotNull(t);
Assert.assertTrue(t instanceofIllegalArgumentException);
Assert.assertTrue(t.getMessage().contains("只能有一种拒绝条件!rejects:2"));
单元测试之Mock技术,Junit结合Mock测试,会发现Eclipse自带的juint和jmock测试异常的时候
报
java.lang.SecurityException: class "org.hamcrest.TypeSafeMatcher"'ssigner information does not match signer information of other classes in thesame package。
添加的时候,注意把JUnit4的order放到最后。因为junit4它自己带了一个Hamcrest jar。
classpath配置如下:
<classpathentrykind="lib" path="lib/jmock-2.5.1.jar"/>
<classpathentry kind="lib"path="lib/jmock-junit3-2.5.1.jar"/>
<classpathentry kind="lib"path="lib/jmock-junit4-2.5.1.jar"/>
<classpathentry kind="lib"path="lib/jmock-legacy-2.5.1.jar"/>
<classpathentryexported="true" kind="con"path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib"path="lib/junit-4.9.0.jar"/>
具体实例如下:
finalEnrollMasService service = context1.mock(EnrollMasService.class);
try {
context1.checking(newExpectations() {
{
oneOf(service).validateInviteCode(with(any(Map.class)));
will(returnValue(newBusinessServiceException()));
}
});
enrollInputAjaxAction.setEnrollMasService(service);
enrollInputAjaxAction.checkMgmInviteCode(request,response);
Assert.assertEquals("-1",response.getModel().get("flag"));
} catch (BusinessServiceExceptione) {
}
ANT 运行Junit 案例 实例,一开始没在Juint 标签里加上 <formattertype="plain"/> ,用例报错之后一直没有日志输出,
加上这句话会输出TEST-xxx.HelloWorldTest.txt这个文件。
建立 java工程的classpath文件
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src"path="src/java"/>
<classpathentry kind="src"path="src/test"/>
<classpathentry kind="con"path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con"path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output"path="bin"/>
</classpath>
<?xml version="1.0" encoding="GB2312"?>
<project name="AntDemo"default="junit" basedir=".">
<!--=================================================================== -->
<!-- 变量设置 -->
<!--=================================================================== -->
<!-- 源代码src路径 -->
<property name="src.path"value="src/java"/>
<!-- 编译文件class路径 -->
<property name="build.path"value="build"/>
<!-- 单元测试代码路径 -->
<property name="test.path"value="src/test"/>
<!-- lib包路径 -->
<property name="lib.path"value="lib"/>
<!--=================================================================== -->
<!-- 设置classpath -->
<!--=================================================================== -->
<pathid="compile.path">
<filesetdir="${lib.path}">
<includename="**/*.jar"/>
</fileset>
<pathelementpath="${build.path}"/>
</path>
<!-- ===================================================================-->
<!-- 清除历史编译class -->
<!--=================================================================== -->
<target name="clean"description="clean">
<deletedir="${build.path}"/>
</target>
<!--=================================================================== -->
<!-- 编译测试文件,初始化目录 -->
<!--=================================================================== -->
<target name="compile"description="compile">
<mkdirdir="${build.path}"/>
<javacsrcdir="${src.path}" destdir="${build.path}" classpathref="compile.path"/>
<javacsrcdir="${test.path}" destdir="${build.path}" classpathref="compile.path"/>
</target>
<!--=================================================================== -->
<!-- 执行测试案例 -->
<!--=================================================================== -->
<target name="junit"depends="clean,compile">
<junitprintsummary="true">
<classpathrefid="compile.path"/>
<formatter type="plain"/>
<test name="XXXX.HelloWorldTest"/>
</junit>
</target>
</project>
在使用ANT执行用例的时候,报错如下Caused an ERROR
org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing
后面把lib的jar 换成4.9.0问题就解决了。
见博客 https://blog.csdn.net/u011954243/article/details/77962329