六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 42|回复: 0

利用easymock及扩展所做测试

[复制链接]

升级  28%

2

主题

2

主题

2

主题

童生

Rank: 1

积分
14
 楼主| 发表于 2013-1-27 05:49:13 | 显示全部楼层 |阅读模式
在面向接口编程单元测试中,可以很简单利用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...
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表