- JUnit 教程
- JUnit - 首页
- JUnit - 概述
- JUnit - 环境设置
- JUnit - 测试框架
- JUnit - 基本用法
- JUnit - API
- JUnit - 编写测试
- JUnit - 使用断言
- JUnit - 执行过程
- JUnit - 执行测试
- JUnit - 套件测试
- JUnit - 忽略测试
- JUnit - 时间测试
- JUnit - 异常测试
- JUnit - 参数化测试
- JUnit - 与 Ant 集成
- JUnit - 与 Eclipse 集成
- JUnit - 扩展
- JUnit 有用资源
- JUnit - 常见问题解答
- JUnit - 快速指南
- JUnit - 有用资源
- JUnit - 讨论
JUnit - 扩展
以下是 JUnit 扩展:
- Cactus
- JWebUnit
- XMLUnit
- MockObject
Cactus
Cactus 是一个简单的测试框架,用于单元测试服务器端 Java 代码(Servlet、EJB、标签库、过滤器)。Cactus 的目的是降低编写服务器端代码测试的成本。它使用 JUnit 并对其进行扩展。Cactus 实现了一种在容器内执行测试的容器内策略。
Cactus 生态系统由多个组件组成:
Cactus 框架是 Cactus 的核心。它是提供 API 来编写 Cactus 测试的引擎。
Cactus 集成模块是前端和框架,它们提供使用 Cactus 框架的简便方法(Ant 脚本、Eclipse 插件和 Maven 插件)。
以下代码演示了如何使用 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().setBaseUrl("http://myserver:8080/myapp");
}
// 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 接口的类)。
通过 DOM 遍历公开的 XML 片段中的各个节点(通过 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 = "<msg><uuid>0x00435A8C</uuid></msg>";
String myTestXML = "<msg><localId>2376</localId></msg>";
assertXMLEqual("Comparing test xml to control xml", myControlXML, myTestXML);
}
}
MockObject
在单元测试中,模拟对象可以模拟复杂、真实的(非模拟)对象的 behavior,因此当在单元测试中包含真实对象不切实际或不可能时,它们非常有用。
使用模拟对象进行测试的常用编码风格是:
- 创建模拟对象的实例。
- 在模拟对象中设置状态和期望。
- 使用模拟对象作为参数调用领域代码。
- 验证模拟对象的一致性。
下面是使用 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();
}
}