junit 框架扩展——迹忆客-ag捕鱼王app官网
以下是 junit 扩展
- cactus
- jwebunit
- xmlunit
- mockobject
cactus
cactus 是一个简单框架用来测试服务器端的 java 代码(servlets, ejbs, tag libs, filters)。cactus 的设计意图是用来减小为服务器端代码写测试样例的成本。它使用 junit 并且在此基础上进行扩展。cactus 实现了 in-container
的策略,意味着可以在容器内部执行测试。
cactus 系统由以下几个部分组成:
- cactus framework(cactus 框架) 是 cactus 的核心。它是提供 api 写 cactus 测试代码的引擎。
- cactus integration modules(cactus 集成模块) 它是提供使用 cactus framework(ant scripts, eclipse plugin, maven plugin)的前端和框架。
这是使用 cactus 的样例代码。
import org.apache.cactus.*;
import junit.framework.*;
public class testsampleservlet extends servlettestcase {
@test
public void testservlet() {
// initialize class to test
sampleservlet servlet = new sampleservlet();
// set a variable in session as the dosomething()
// method that we are testing
session.setattribute("name", "value");
// call the method to test, passing an
// httpservletrequest object (for example)
string result = servlet.dosomething(request);
// perform verification that test was successful
assertequals("something", result);
assertequals("othervalue", session.getattribute("othername"));
}
}
jwebunit
jwebunit 是一个基于 java 的用于 web 应用的测试框架。它以一种统一、简单测试接口的方式包装了如 htmlunit 和 selenium 这些已经存在的框架来允许你快速地测试 web 应用程序的正确性。
jwebunit 提供了一种高级别的 java api 用来处理结合了一系列验证程序正确性的断言的 web 应用程序。这包括通过链接,表单的填写和提交,表格内容的验证和其他 web 应用程序典型的业务特征。
这个简单的导航方法和随时可用的断言允许建立更多的快速测试而不是仅仅使用 junit 和 htmlunit。另外如果你想从 htmlunit 切换到其它的插件,例如 selenium(很快可以使用),那么不用重写你的测试样例代码。
以下是样例代码。
import junit.framework.testcase;
import net.sourceforge.jwebunit.webtester;
public class examplewebtestcase extends testcase {
private webtester tester;
public examplewebtestcase(string name) {
super(name);
tester = new webtester();
}
//set base url
public void setup() throws exception {
gettestcontext().setbase;
}
// test base info
@test
public void testinfopage() {
beginat("/info.html");
}
}
xmlunit
xmlunit 提供了一个单一的 junit 扩展类,即 xmltestcase
,还有一些允许断言的支持类:
- 比较两个 xml 文件的不同(通过使用 diff 和 detaileddiff 类)
- 一个 xml 文件的验证(通过使用 validator 类)
- 使用 xslt 转换一个 xml 文件的结果(通过使用 transform 类)
- 对一个 xml 文件 xpath 表达式的评估(通过实现 xpathengine 接口)
- 一个 xml 文件进行 dom traversal 后的独立结点(通过使用 nodetest 类)
我们假设有两个我们想要比较和断言它们相同的 xml 文件,我们可以写一个如下的简单测试类:
import org.custommonkey.xmlunit.xmltestcase;
public class myxmltestcase extends xmltestcase {
// this test method compare two pieces of the xml
@test
public void testforxmlequality() throws exception {
string mycontrolxml = "0x00435a8c ";
string mytestxml = "2376 ";
assertxmlequal("comparing test xml to control xml",
mycontrolxml, mytestxml);
}
}
mockobject
在一个单元测试中,虚拟对象可以模拟复杂的,真实的(非虚拟)对象的行为,因此当一个真实对象不现实或不可能包含进一个单元测试的时候非常有用。
用虚拟对象进行测试时一般的编程风格包括:
- 创建虚拟对象的实例
- 在虚拟对象中设置状态和描述
- 结合虚拟对象调用域代码作为参数
- 在虚拟对象中验证一致性
以下是使用 jmock 的 mockobject 例子。
import org.jmock.mockery;
import org.jmock.expectations;
class pubtest extends testcase {
mockery context = new mockery();
public void testsubreceivesmessage() {
// set up
final sub sub = context.mock(sub.class);
pub pub = new pub();
pub.add(sub);
final string message = "message";
// expectations
context.checking(new expectations() {
oneof (sub).receive(message);
});
// execute
pub.publish(message);
// verify
context.assertissatisfied();
}
}