浏览 3420 次
锁定老帖子 主题:利用easymock及扩展所做测试
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-10-09
下面是利用扩展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... 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |