JUnit In Action 学习笔记
2007-10-15 16:46
435 查看
(1)回归测试----在已经存在的代码中加入新的代码时以保证代码仍然能够正常运行的测试叫回归测试(regression test)。
(2)Ant
// P124页
// 后面还有一个生成 xml 格式的 report 说明
// 自动找出要运行的测试
// 给项目命名,并设定黙认目标以便测试
<project name="sampling" default="test">
// 你包含一个 build.properties 文件
// 这个文件包括由于依赖于执行文件而必须在用户的系统上改变的 Ant 属性。
<property file="build.properties" />
<property name="src.dir" location="src" />
<property name="src.java.dir" location="${src.dir}/java" />
<property name="src.test.dir" location="${src.dir}/test" />
<property name="target.dir" location="target" />
<property name="target.classes.java.dir" location="${target.dir}/classes/java" />
<property name="target.classes.test.dir" location="${target.dir}/classes/test" />
// 定义一个名为 compile.java 的目标来编译 java 产品源码
<target name="compile.java">
// 保证产生产品类文件的目录存在
<mkdir dir="${target.classes.java.dir}" />
// 调用 java 编译器(javac)并且传给它目标目录
<javac destdir="${target.classes.java.dir}">
// 告诉 javac 需要编译的源码在哪
<src path="${src.java.dir}" />
</javac>
</target>
// 目标 compile.test 依赖于目标 compile.java
<target name="compile.test" depends="compile.java">
<mkdir dir="${target.classes.test.dir}" />
<javac destdir="${target.classes.test.dir}">
<src path="${src.test.dir}" />
// 你得添加一条嵌套的 classpath 元素
<classpath>
<pathelement location="${target.classes.java.dir}" />
</classpath>
</javac>
</target>
// 创建一个自动调用目标 compile.java 和 compile.test 编译目标
<target name="compile" depends="compile.java,compile.test" />
// 构建文件的 test 目标
// 如果你让 Ant 运行测试目标,它会先运行编译目标
<target name="test" depends="compile">
// printsummary 属性表明在测试的最后一行输出一个单行的概要
// 通过把 fork 设为 yes,你强制 Ant 对每个测试分别使用一个单独的 java 虚拟机(JVM)
// 属性 haltonerror 和 haltonfailure 表明如果失败或产生错误将停止编译
<junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">
// 配置 junit 任务格式器,用纯文本并且输出文本到控制台
<formatter type="plain" usefile="false" />
<test name="junitbook.sampling.TestDefaultController" />
<classpath>
<pathelement location="${target.classes.java.dir}" />
<pathelement location="${target.classes.test.dir}" />
</classpath>
</junit>
</target>
</project>
(3)stub 可以用来代替文件系统、到服务器的连接、数据库等。mock objects 可以用来代替单一的类
(4)Jetty
package edu.dhu.zkl.jetty;
import org.mortbay.http.HttpContext;
import org.mortbay.http.HttpServer;
import org.mortbay.http.SocketListener;
import org.mortbay.http.handler.ResourceHandler;
public class JettySample {
public static void main(String[] args) throws Exception {
HttpServer server = new HttpServer();
SocketListener listener = new SocketListener();
listener.setPort(8081);
server.addListener(listener);
HttpContext context = new HttpContext();
context.setContextPath("/");
context.setResourceBase("./");
context.addHandler( new ResourceHandler() );
server.addContext(context);
server.start();
}
}
运行这个程序,然后在浏览器输入 http://localhost:8081/ 就可以看到项目下的所有资源。
(5)用 Jetty 来演示一个 Stub 的测试(注意加入 servelt-api.jar 和 jetty.jar )
两个类,一个 TestWebClientSetup1 用来内嵌 Jetty 服务器,另一个 TestWebClient1 简单的测试案例
package edu.dhu.zkl.jetty.test;
import java.io.IOException;
import java.io.OutputStream;
import junit.extensions.TestSetup;
import junit.framework.Test;
import org.mortbay.http.HttpContext;
import org.mortbay.http.HttpFields;
import org.mortbay.http.HttpRequest;
import org.mortbay.http.HttpResponse;
import org.mortbay.http.HttpServer;
import org.mortbay.http.SocketListener;
import org.mortbay.http.handler.AbstractHttpHandler;
import org.mortbay.http.handler.NotFoundHandler;
import org.mortbay.util.ByteArrayISO8859Writer;
public class TestWebClientSetup1 extends TestSetup {
protected static HttpServer server;
public TestWebClientSetup1(Test suite) {
super(suite);
}
protected void setUp() throws Exception {
server = new HttpServer();
SocketListener listener = new SocketListener();
listener.setPort(8081);
server.addListener(listener);
HttpContext context1 = new HttpContext();
context1.setContextPath("/testGetContentOk");
context1.addHandler(new TestGetContentOkHandler());
server.addContext(context1);
HttpContext context2 = new HttpContext();
context2.setContextPath("/testGetContentNotFound");
context2.addHandler(new NotFoundHandler());
server.addContext(context2);
HttpContext context3 = new HttpContext();
context3.setContextPath("/testGetContentServerError");
context3.addHandler(new TestGetContentServerErrorHandler());
server.addContext(context3);
server.start();
}
protected void tearDown() throws Exception {
server.stop();
}
private class TestGetContentOkHandler extends AbstractHttpHandler {
/**
*
*/
private static final long serialVersionUID = 1L;
public void handle( String pathInContext, String pathParams,
HttpRequest request, HttpResponse response ) throws IOException {
OutputStream out = response.getOutputStream();
ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
writer.write("It works");
writer.flush();
response.setIntField( HttpFields.__ContentLength, writer.size() );
writer.writeTo(out);
out.flush();
request.setHandled(true);
}
}
private class TestGetContentServerErrorHandler extends AbstractHttpHandler {
/**
*
*/
private static final long serialVersionUID = 1L;
public void handle(String pathInContext, String pathParams,
HttpRequest request, HttpResponse response ) throws IOException {
response.sendError(HttpResponse.__503_Service_Unavailable);
}
}
}
package edu.dhu.zkl.j
4000
etty.test;
import java.net.URL;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import edu.dhu.zkl.jetty.WebClient;
public class TestWebClient1 extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(TestWebClient1.class);
return new TestWebClientSetup1(suite);
}
public void testGetContentOk() throws Exception {
WebClient client = new WebClient();
String result = client.getContent(new URL(
"http://localhost:8081/testGetContentOk" ) );
assertEquals( "It works", result );
}
public void testGetContentNotFound() throws Exception {
WebClient client = new WebClient();
String result = client.getContent(new URL(
"http://localhost:8081/testGetContentNotFound" ) );
assertNull(result);
}
public void testGetContentServerError() throws Exception {
WebClient client = new WebClient();
String result = client.getContent(new URL(
"http://localhost:8081/testGetContentServerError" ) );
assertNull(result);
}
}
还有一个 WebClient 类,被测类
package edu.dhu.zkl.jetty;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.IOException;
public class WebClient
{
public String getContent(URL url)
{
StringBuffer content = new StringBuffer();
try
{
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setDoInput(true);
InputStream is = connection.getInputStream();
byte[] buffer = new byte[2048];
int count;
while (-1 != (count = is.read(buffer)))
{
content.append(new String(buffer, 0, count));
}
}
catch (IOException e)
{
return null;
}
return content.toString();
}
}
(6)
(2)Ant
// P124页
// 后面还有一个生成 xml 格式的 report 说明
// 自动找出要运行的测试
// 给项目命名,并设定黙认目标以便测试
<project name="sampling" default="test">
// 你包含一个 build.properties 文件
// 这个文件包括由于依赖于执行文件而必须在用户的系统上改变的 Ant 属性。
<property file="build.properties" />
<property name="src.dir" location="src" />
<property name="src.java.dir" location="${src.dir}/java" />
<property name="src.test.dir" location="${src.dir}/test" />
<property name="target.dir" location="target" />
<property name="target.classes.java.dir" location="${target.dir}/classes/java" />
<property name="target.classes.test.dir" location="${target.dir}/classes/test" />
// 定义一个名为 compile.java 的目标来编译 java 产品源码
<target name="compile.java">
// 保证产生产品类文件的目录存在
<mkdir dir="${target.classes.java.dir}" />
// 调用 java 编译器(javac)并且传给它目标目录
<javac destdir="${target.classes.java.dir}">
// 告诉 javac 需要编译的源码在哪
<src path="${src.java.dir}" />
</javac>
</target>
// 目标 compile.test 依赖于目标 compile.java
<target name="compile.test" depends="compile.java">
<mkdir dir="${target.classes.test.dir}" />
<javac destdir="${target.classes.test.dir}">
<src path="${src.test.dir}" />
// 你得添加一条嵌套的 classpath 元素
<classpath>
<pathelement location="${target.classes.java.dir}" />
</classpath>
</javac>
</target>
// 创建一个自动调用目标 compile.java 和 compile.test 编译目标
<target name="compile" depends="compile.java,compile.test" />
// 构建文件的 test 目标
// 如果你让 Ant 运行测试目标,它会先运行编译目标
<target name="test" depends="compile">
// printsummary 属性表明在测试的最后一行输出一个单行的概要
// 通过把 fork 设为 yes,你强制 Ant 对每个测试分别使用一个单独的 java 虚拟机(JVM)
// 属性 haltonerror 和 haltonfailure 表明如果失败或产生错误将停止编译
<junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">
// 配置 junit 任务格式器,用纯文本并且输出文本到控制台
<formatter type="plain" usefile="false" />
<test name="junitbook.sampling.TestDefaultController" />
<classpath>
<pathelement location="${target.classes.java.dir}" />
<pathelement location="${target.classes.test.dir}" />
</classpath>
</junit>
</target>
</project>
(3)stub 可以用来代替文件系统、到服务器的连接、数据库等。mock objects 可以用来代替单一的类
(4)Jetty
package edu.dhu.zkl.jetty;
import org.mortbay.http.HttpContext;
import org.mortbay.http.HttpServer;
import org.mortbay.http.SocketListener;
import org.mortbay.http.handler.ResourceHandler;
public class JettySample {
public static void main(String[] args) throws Exception {
HttpServer server = new HttpServer();
SocketListener listener = new SocketListener();
listener.setPort(8081);
server.addListener(listener);
HttpContext context = new HttpContext();
context.setContextPath("/");
context.setResourceBase("./");
context.addHandler( new ResourceHandler() );
server.addContext(context);
server.start();
}
}
运行这个程序,然后在浏览器输入 http://localhost:8081/ 就可以看到项目下的所有资源。
(5)用 Jetty 来演示一个 Stub 的测试(注意加入 servelt-api.jar 和 jetty.jar )
两个类,一个 TestWebClientSetup1 用来内嵌 Jetty 服务器,另一个 TestWebClient1 简单的测试案例
package edu.dhu.zkl.jetty.test;
import java.io.IOException;
import java.io.OutputStream;
import junit.extensions.TestSetup;
import junit.framework.Test;
import org.mortbay.http.HttpContext;
import org.mortbay.http.HttpFields;
import org.mortbay.http.HttpRequest;
import org.mortbay.http.HttpResponse;
import org.mortbay.http.HttpServer;
import org.mortbay.http.SocketListener;
import org.mortbay.http.handler.AbstractHttpHandler;
import org.mortbay.http.handler.NotFoundHandler;
import org.mortbay.util.ByteArrayISO8859Writer;
public class TestWebClientSetup1 extends TestSetup {
protected static HttpServer server;
public TestWebClientSetup1(Test suite) {
super(suite);
}
protected void setUp() throws Exception {
server = new HttpServer();
SocketListener listener = new SocketListener();
listener.setPort(8081);
server.addListener(listener);
HttpContext context1 = new HttpContext();
context1.setContextPath("/testGetContentOk");
context1.addHandler(new TestGetContentOkHandler());
server.addContext(context1);
HttpContext context2 = new HttpContext();
context2.setContextPath("/testGetContentNotFound");
context2.addHandler(new NotFoundHandler());
server.addContext(context2);
HttpContext context3 = new HttpContext();
context3.setContextPath("/testGetContentServerError");
context3.addHandler(new TestGetContentServerErrorHandler());
server.addContext(context3);
server.start();
}
protected void tearDown() throws Exception {
server.stop();
}
private class TestGetContentOkHandler extends AbstractHttpHandler {
/**
*
*/
private static final long serialVersionUID = 1L;
public void handle( String pathInContext, String pathParams,
HttpRequest request, HttpResponse response ) throws IOException {
OutputStream out = response.getOutputStream();
ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
writer.write("It works");
writer.flush();
response.setIntField( HttpFields.__ContentLength, writer.size() );
writer.writeTo(out);
out.flush();
request.setHandled(true);
}
}
private class TestGetContentServerErrorHandler extends AbstractHttpHandler {
/**
*
*/
private static final long serialVersionUID = 1L;
public void handle(String pathInContext, String pathParams,
HttpRequest request, HttpResponse response ) throws IOException {
response.sendError(HttpResponse.__503_Service_Unavailable);
}
}
}
package edu.dhu.zkl.j
4000
etty.test;
import java.net.URL;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import edu.dhu.zkl.jetty.WebClient;
public class TestWebClient1 extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(TestWebClient1.class);
return new TestWebClientSetup1(suite);
}
public void testGetContentOk() throws Exception {
WebClient client = new WebClient();
String result = client.getContent(new URL(
"http://localhost:8081/testGetContentOk" ) );
assertEquals( "It works", result );
}
public void testGetContentNotFound() throws Exception {
WebClient client = new WebClient();
String result = client.getContent(new URL(
"http://localhost:8081/testGetContentNotFound" ) );
assertNull(result);
}
public void testGetContentServerError() throws Exception {
WebClient client = new WebClient();
String result = client.getContent(new URL(
"http://localhost:8081/testGetContentServerError" ) );
assertNull(result);
}
}
还有一个 WebClient 类,被测类
package edu.dhu.zkl.jetty;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.IOException;
public class WebClient
{
public String getContent(URL url)
{
StringBuffer content = new StringBuffer();
try
{
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setDoInput(true);
InputStream is = connection.getInputStream();
byte[] buffer = new byte[2048];
int count;
while (-1 != (count = is.read(buffer)))
{
content.append(new String(buffer, 0, count));
}
}
catch (IOException e)
{
return null;
}
return content.toString();
}
}
(6)
相关文章推荐
- junit in action 学习笔记
- junit in action 学习笔记
- jmx in action 学习笔记(2)
- JUnit学习笔记
- JUnit学习笔记
- [Hadoop In Action]学习笔记-Hadoop中的Streaming
- 【Puppet in action】 学习笔记之Puppet目录结构及配置文件说明
- JUnit学习笔记(2007版)
- junit4学习笔记
- JUnit学习笔记6---用stub进行粗粒度测试
- JUnit测试驱动开发学习笔记1
- junit学习笔记(二)
- JUnit学习笔记之NetBeans篇
- JUnit 学习笔记 第二记
- 机器学习实战(Machine Learning in Action)笔记--Chapter1:机器学习基础
- AJAX IN ACTION 学习笔记(三)
- JUnit学习笔记18---对数据库应用程序进行单元测试2
- JUnit学习笔记15---对JSP和taglib进行单元测试2
- junit学习笔记
- JUnit学习笔记19---对数据库应用程序进行单元测试3