论坛首页 综合技术论坛

利用easymock及扩展所做测试

浏览 3420 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-10-09  
在面向接口编程单元测试中,可以很简单利用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...
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics