- 浏览: 191176 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (83)
- J2EE/Core Java (24)
- J2EE/Portal (2)
- J2EE/UI (4)
- J2EE/ATG (1)
- J2EE/Report (1)
- J2EE/Web Service/Rest API (2)
- Design Pattern (2)
- Arithmetic (4)
- Linux (12)
- Ruby&Rails (17)
- Database (5)
- J2EE/Payment (1)
- J2EE/JVM (1)
- Encryption/Decryption (3)
- J2EE/Multi Threading (4)
- SQL (1)
- https://community.teamviewer.com/t5/Knowledge-Base/Where-can-I-download-older-TeamViewer-versions-nbsp/ta-p/7729 (0)
最新评论
Maven Installation+
add following code to pom.xml
EasyMock Basic Flow
Usage
The following examples use the interface Collaborator:
Implementors of this interface are collaborators (in this case listeners) of a class named ClassUnderTest:
Official quick start
http://easymock.org/EasyMock3_0_Documentation.html
EasyMock3.0 API
http://easymock.org/api/easymock/3.0/index.html
add following code to pom.xml
<dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.0</version> <scope>test</scope> </dependency>
EasyMock Basic Flow
引用
1 create Mock object by EasyMock;
or
2 set the expected behavior and Return Results;
3 Switch the esaymock to replay state;
or
4 do unit test with mock object;
5 verify
SomeInterface mockObj = createMock(SomeInterface.class);
or
引用
IMocksControl control = createControl();
SomeInterface mockObj = control.createMock(SomeInterface.class);
AnotherInterface mockAnotherObj = control.createMock(AnotherInterface.class);
SomeInterface mockObj = control.createMock(SomeInterface.class);
AnotherInterface mockAnotherObj = control.createMock(AnotherInterface.class);
2 set the expected behavior and Return Results;
expect(mockObj.someAction(1)).andReturn("one");
3 Switch the esaymock to replay state;
replay(mockObj);
or
control.replay();
4 do unit test with mock object;
5 verify
verify(mockObj);
Usage
The following examples use the interface Collaborator:
package org.easymock.samples; public interface Collaborator { void documentAdded(String title); void documentChanged(String title); void documentRemoved(String title); byte voteForRemoval(String title); byte[] voteForRemovals(String[] title); }
Implementors of this interface are collaborators (in this case listeners) of a class named ClassUnderTest:
package org.easymock.samples; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class ClassTested { private final Set<Collaborator> listeners = new HashSet<Collaborator>(); private final Map<String, byte[]> documents = new HashMap<String, byte[]>(); public void addListener(final Collaborator listener) { listeners.add(listener); } public void addDocument(final String title, final byte[] document) { final boolean documentChange = documents.containsKey(title); documents.put(title, document); if (documentChange) { notifyListenersDocumentChanged(title); } else { notifyListenersDocumentAdded(title); } } public boolean removeDocument(final String title) { if (!documents.containsKey(title)) { return true; } if (!listenersAllowRemoval(title)) { return false; } documents.remove(title); notifyListenersDocumentRemoved(title); return true; } public boolean removeDocuments(final String... titles) { if (!listenersAllowRemovals(titles)) { return false; } for (final String title : titles) { documents.remove(title); notifyListenersDocumentRemoved(title); } return true; } private void notifyListenersDocumentAdded(final String title) { for (final Collaborator listener : listeners) { listener.documentAdded(title); } } private void notifyListenersDocumentChanged(final String title) { for (final Collaborator listener : listeners) { listener.documentChanged(title); } } private void notifyListenersDocumentRemoved(final String title) { for (final Collaborator listener : listeners) { listener.documentRemoved(title); } } private boolean listenersAllowRemoval(final String title) { int result = 0; for (final Collaborator listener : listeners) { result += listener.voteForRemoval(title); } return result > 0; } private boolean listenersAllowRemovals(final String... titles) { int result = 0; for (final Collaborator listener : listeners) { result += listener.voteForRemovals(titles); } return result > 0; } }
package org.easymock.samples; import static org.easymock.EasyMock.*; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import org.easymock.IAnswer; import org.junit.Before; import org.junit.Test; public class ExampleTest { private ClassTested classUnderTest; private Collaborator mock; @Before public void setup() { mock = createMock(Collaborator.class); classUnderTest = new ClassTested(); classUnderTest.addListener(mock); } @Test public void removeNonExistingDocument() { replay(mock); classUnderTest.removeDocument("Does not exist"); } @Test public void addDocument() { mock.documentAdded("New Document"); replay(mock); classUnderTest.addDocument("New Document", new byte[0]); verify(mock); } @Test public void addAndChangeDocument() { mock.documentAdded("Document"); mock.documentChanged("Document"); expectLastCall().times(3); replay(mock); classUnderTest.addDocument("Document", new byte[0]); classUnderTest.addDocument("Document", new byte[0]); classUnderTest.addDocument("Document", new byte[0]); classUnderTest.addDocument("Document", new byte[0]); verify(mock); } @Test public void voteForRemoval() { // expect document addition mock.documentAdded("Document"); // expect to be asked to vote, and vote for it expect(mock.voteForRemoval("Document")).andReturn((byte) 42); // expect document removal mock.documentRemoved("Document"); replay(mock); classUnderTest.addDocument("Document", new byte[0]); assertTrue(classUnderTest.removeDocument("Document")); verify(mock); } @Test public void voteAgainstRemoval() { // expect document addition mock.documentAdded("Document"); // expect to be asked to vote, and vote against it expect(mock.voteForRemoval("Document")).andReturn((byte) -42); // // document removal is *not* expected replay(mock); classUnderTest.addDocument("Document", new byte[0]); assertFalse(classUnderTest.removeDocument("Document")); verify(mock); } @Test public void voteForRemovals() { mock.documentAdded("Document 1"); mock.documentAdded("Document 2"); expect(mock.voteForRemovals("Document 1", "Document 2")).andReturn( (byte) 42); mock.documentRemoved("Document 1"); mock.documentRemoved("Document 2"); replay(mock); classUnderTest.addDocument("Document 1", new byte[0]); classUnderTest.addDocument("Document 2", new byte[0]); assertTrue(classUnderTest.removeDocuments(new String[] { "Document 1", "Document 2" })); verify(mock); } @Test public void voteAgainstRemovals() { mock.documentAdded("Document 1"); mock.documentAdded("Document 2"); expect(mock.voteForRemovals("Document 1", "Document 2")).andReturn( (byte) -42); replay(mock); classUnderTest.addDocument("Document 1", new byte[0]); classUnderTest.addDocument("Document 2", new byte[0]); assertFalse(classUnderTest.removeDocuments("Document 1", "Document 2")); verify(mock); } @SuppressWarnings("unchecked") @Test public void answerVsDelegate() { final List<String> l = createMock(List.class); // andAnswer style expect(l.remove(10)).andAnswer(new IAnswer<String>() { public String answer() throws Throwable { return getCurrentArguments()[0].toString(); } }); // andDelegateTo style expect( l.remove(10)). andDelegateTo ( new ArrayList<String>() { private static final long serialVersionUID = 1L; @Override public String remove(final int index) { return Integer.toString(index); } } ); replay(l); assertEquals("10", l.remove(10)); assertEquals("10", l.remove(10)); verify(l); } }
Official quick start
http://easymock.org/EasyMock3_0_Documentation.html
EasyMock3.0 API
http://easymock.org/api/easymock/3.0/index.html
- easymock-3.0.zip (6.8 MB)
- 下载次数: 5
- endeca.zip (1.4 MB)
- 下载次数: 6
发表评论
-
Start tomcat with port 80 without Linux root user-Use iptables mapping
2016-05-25 17:39 885引用In linux system. only root us ... -
Format XML in JAVA
2016-01-11 12:23 632public static String format ... -
HttpURLConnection下载文件
2015-08-07 11:25 838public class HttpDownloadUtilit ... -
Ehcache RMI Replicated Cluster(RMI集群)
2013-04-25 23:39 1104引用本文是ehcache RMI集群的例子,导入附件中的jav ... -
Integrete unitils for database(dao) testing
2013-02-01 18:39 1730引用Database testing Unit tests f ... -
JAXB入门
2012-10-16 11:59 825引用jaxb是一个读写xml的工具,还可以提供验证,不需要额外 ... -
Freemarker使用入门
2012-10-16 11:54 1059引用freemarker是一种模板标记工具,可以做页面静态化, ... -
perforce java api使用
2012-10-16 11:43 1291引用perforce是种版本管理软件,提供啦完整的java a ... -
XPath 入门
2012-10-16 11:29 916引用xpath可以快速定位获取XML文件中指定属性和值,jdk ... -
Java File Diff-diffutils
2012-09-27 17:35 75561. Maven Dependency <depende ... -
XSD 入门使用
2012-09-18 23:20 820<?xml version="1.0" ... -
nexus-2.1.1安装及使用入门
2012-08-13 22:52 14941. 安装 地址http://www.sonatype.org ... -
File Demo
2012-06-25 22:55 1359package org.springside.examples ... -
Java 访问sharepoint webservice(NTLM & SSL)
2012-06-12 09:47 3811引用遇到需要使用java访问微软的sharepoint的web ... -
Selenium Web Driver入门
2012-05-27 23:17 58921 What is Selenium? 引用Selenium ... -
HttpClient4.1.2 & HtmlUnit2.9 处理文件下载
2012-01-09 18:18 1076TestCode import java.io.Fi ... -
HttpClient4.1.2 & HtmlUnit2.9 NTLM 验证 和 Httpclient4.1.2 https/SSL
2012-01-09 18:13 16461. HttpClient4.1.2 & HtmlUn ... -
HttpClient4登陆ITeye
2012-01-08 23:33 1934import java.io.IOException; im ... -
Spring2集成测试
2011-08-25 22:21 800Spring2测试类继承层次 集成测试例子 public ... -
Maven+jetty+jrebel+m2eclipse+eclipse搭建struts2开发环境
2011-08-11 11:18 4187引用Maven:项目构建工具,通过pom.xml可以自动维护j ...
相关推荐
本资源包含JMock 2.5.1和EasyMock 3.0的库文件,使得开发者在进行单元测试时无需再四处寻找相关依赖。 **JMock 2.5.1** JMock是一个强大的模拟框架,它允许开发者创建和控制模拟对象,这些对象可以在测试中替代真实...
标题中的"junit4.4.jar +easymock3.0.jar"提及了两个重要的Java测试库:JUnit 4.4和EasyMock 3.0。这些是用于开发过程中的单元测试的关键工具。 JUnit是Java编程语言中最广泛使用的单元测试框架之一。它允许开发者...
有用的jar包 是我好不容易找到的 现在分享给大家了
Easymock 3.0 是一个流行的Java模拟框架,用于进行单元测试。它使得开发者能够在测试代码中创建和控制对象的行为,以便于隔离被测试代码并确保其正确性。在单元测试中,Easymock允许我们创建mock对象,这些对象模仿...
java运行依赖jar包
easymockclassextension-3.0.jar org.easymock.classextension.EasyMock.
Easymock是一个流行的Java单元测试框架,它允许开发者创建模拟对象来测试代码。这个框架使得测试更加简单,因为你可以模拟任何复杂的交互和行为,而无需实际运行依赖的组件。在给定的压缩包文件中,包含两个核心的...
jar包,官方版本,自测可用
jar包,官方版本,自测可用
注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...
在提供的`easymock.rar`和`easymock-3.0.zip`文件中,可能包含了EasyMock的库文件和相关的示例代码。你可以解压并导入到你的项目中,参考示例来学习和使用EasyMock。在实际应用中,结合IDE(如Eclipse或IntelliJ ...
EasyMock 是一个强大的Java模拟框架,它允许开发者在单元测试中创建和控制对象的行为,以模拟复杂的交互场景。这个框架的出现,极大地简化了对那些难以或无法直接实例化的类的测试,比如接口或者静态方法。EasyMock...
EasyMock 是一个强大的Java模拟框架,它允许开发者在单元测试中创建和控制对象的行为,以模拟复杂的依赖关系。这个框架的出现使得测试更加简洁、独立,可以有效地验证代码的正确性,而无需运行实际的依赖服务或库。...
jar包,官方版本,自测可用
EasyMock 3.2 是一个流行的开源Java模拟框架,它为开发者提供了强大的单元测试支持。在Java开发中,单元测试是验证代码独立模块正确性的关键步骤。EasyMock可以帮助程序员模拟对象的行为,使得测试过程更加可控,...
EasyMock主要是为测试提供模拟数据,比如你可以模拟HttpServletRequest。
【EasyMock介绍】 EasyMock是一款强大的Java模拟框架,它允许开发者在进行单元测试时创建和控制对象的行为。这个工具使得测试更加独立,可以隔离被测试代码与其他依赖的系统,从而提高测试的效率和质量。EasyMock的...