利用easymock及扩展所做测试

在面向接口编程单元测试中,可以很简单利用easymock来mock一个模拟对象,但如果,要测试的是一个抽象类是,就不能直接利用 easymock来做,需要下载easymock的扩展包,下载地址:http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/e/ea/easymock/easymockclassextension2.2.zip (这是写此文章时最新版本).
下面是利用扩展easymock构造单元测试的例子:

/*
* Copyright (c) 2003-2006 OFFIS, Henri Tremblay.
* This program is made available under the terms of the MIT License.
*/
package org.easymock.classextension.samples;

import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import junit.framework.TestCase;

/**
* Example of how to use <code>MockClassControl</code>
*/
public class BasicClassMockTest extends TestCase {
/**
* Our nice class that is allowed to print
*/
public static class Document {
private Printer printer;
private String content;
public Document(Printer printer) {
this.printer = printer;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public void print() {
printer.print(content);
}
}

/**
* The terrible 3rd party class that is not an interface but that we realy
* want to mock.
*/
public static abstract class Printer {
public abstract void print(String toPrint);
}

private Printer printer;
private Document document;
protected void setUp() throws Exception {
super.setUp();
printer = createMock(Printer.class);;
document = new Document(printer);
}
protected void tearDown() throws Exception {
super.tearDown();
printer = null;
document = null;
}

public void testPrintContent() {
printer.print("Hello world");
replay(printer);

document.setContent("Hello world");
document.print();

verify(printer); // make sure Printer.print was called
}

public void testPrintEmptyContent() {
printer.print("");
replay(printer);

document.setContent("");
document.print();

verify(printer); // make sure Printer.print was called
}
}


例子二:
/*
* Copyright (c) 2003-2006 OFFIS, Henri Tremblay.
* This program is made available under the terms of the MIT License.
*/
package org.easymock.classextension.samples;

import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;

import java.lang.reflect.Method;

import junit.framework.TestCase;

/**
* @author Henri Tremblay
*/
public class PartialClassMockTest extends TestCase {

public static class Rect {

private int x;

private int y;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getArea() {
return getX() * getY();
}
}

private Rect rect;

protected void setUp() throws Exception {
super.setUp();
rect = createMock(Rect.class, new Method[] {
Rect.class.getMethod("getX", (Class[]) null),
Rect.class.getMethod("getY", (Class[]) null)});
}

protected void tearDown() throws Exception {
super.tearDown();
rect = null;
}

public void testGetArea() {
expect(rect.getX()).andReturn(4);
expect(rect.getY()).andReturn(5);
replay(rect);
assertEquals(20, rect.getArea());
verify(rect);
}
}
.......................................Power by ankor....2007.10.9...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值