- 浏览: 1015311 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (1355)
- test (75)
- 红茶和绿茶 (1)
- Jave SE (206)
- Oracle (19)
- English (177)
- Log4j (5)
- RIA(Rich Internet Applications) (9)
- Ext Js (6)
- Android (14)
- Logo (0)
- 文字采撷 (287)
- 使用技巧 (92)
- Project Management (22)
- Hibernate (12)
- Struts (5)
- 规则引擎 (1)
- Html & Javasctipt (56)
- Spring MVC (10)
- Maven (17)
- Java Test (17)
- Linux (16)
- Tools (1)
- CV (0)
- Middleware (2)
- HTML5 (2)
- Algorithms (4)
- Web Service (15)
- 留学 (15)
- LADP (5)
- PXCOA (0)
- SysLog (6)
- SSO (3)
- Spring Security (4)
- Spring Batch (1)
- Jmail (1)
- Bible (4)
- Java Thread (5)
- Architect (6)
- github (2)
- Java Swing (12)
- NoSQL (7)
- UML (2)
- 敏捷(Agile) (7)
- Hudson+Maven+SVN (15)
- cloud computing (2)
- Bahasa Indonesia (1)
- jBPM (6)
- 民俗知识 (3)
- Consulting (1)
- Mysql (5)
- SAP (1)
- 微信公众平台接口开发 (3)
- 做生意 (1)
- 西餐 (1)
- Banking (1)
- Flex (0)
- 黄金投资 (1)
- Apache Tomcat 集群 (3)
- Hadoop (7)
- 需求分析 (1)
- 银行知识 (3)
- 产品管理 (2)
- 钢琴Music (3)
- 设计 (3)
- Marketing (2)
- US Life (3)
- 算法 (14)
- BigData (4)
- test红茶和绿茶Jave SEOracleEnglishLog4jRIA(Rich Internet Applications)Ext JsAndroidLogo文字采撷 (0)
- Design Pattern (5)
- NodeJS&AngularJS (9)
- Python (1)
- Spring boot (0)
- ACM (3)
最新评论
-
心往圣城:
微时代-最专业的微信第三方平台。LBS定位导航,微网站,自定义 ...
微信公众平台 /微信公众平台怎么用 -
zhaojiafan:
return ReverseStr1(str.substrin ...
逆转字符串 Write a String Reverser (and use Recursion!) -
zhaojiafan:
public class StringUtils {
p ...
逆转字符串 Write a String Reverser (and use Recursion!)
最近想玩玩JMock。
对着官方文档,想开始写个test case,不过让我郁闷的是官方文档上给的实例代码不完全。算了,自己写个跑跑看了。
1.测试接口:
IHelloService:
2.测试类:
HelloService:
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
对着官方文档,想开始写个test case,不过让我郁闷的是官方文档上给的实例代码不完全。算了,自己写个跑跑看了。
1.测试接口:
IHelloService:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->1 public interface IHelloService {
2
3 /**
4 * @param name
5 * @return hello message
6 */
7 String sayHelloToSomebody(String name);
8
9 }
IMPL:2
3 /**
4 * @param name
5 * @return hello message
6 */
7 String sayHelloToSomebody(String name);
8
9 }
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1 public class HelloServiceImpl implements IHelloService {
2
3 /*
4 * (non-Javadoc)
5 *
6 * @see org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String,
7 * java.lang.String)
8 */
9 public String sayHelloToSomebody(String name) {
10 return "HELLO," + name + "!";
11 }
12
13 }
Test Case:2
3 /*
4 * (non-Javadoc)
5 *
6 * @see org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String,
7 * java.lang.String)
8 */
9 public String sayHelloToSomebody(String name) {
10 return "HELLO," + name + "!";
11 }
12
13 }
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1 public class IHelloServiceTest extends TestCase {
2 private Mockery context = new JUnit4Mockery();
3 private IHelloService helloService;
4
5 /**
6 * @throws java.lang.Exception
7 */
8 @Before
9 public void setUp() throws Exception {
10 // set up
11 helloService = context.mock(IHelloService.class);
12 }
13
14 /**
15 * Test method for
16 * {@link org.hook.jmock.firstcase.HelloServiceImpl#sayHelloToSomebody(java.lang.String)}.
17 */
18 @Test
19 public void testSayHelloToSomebody() {
20 final String message = "HELLO,alex!";
21 final String name = "alex";
22 // expectations
23 context.checking(new Expectations() {
24 {
25 one(helloService).sayHelloToSomebody(name);
26 will(returnValue(message));
27 }
28 });
29 // execute
30 String result = helloService.sayHelloToSomebody(name);
31 // verify
32 context.assertIsSatisfied();
33 assertSame(result, message);
34 }
35 }
OK,跑下测试,green bar...2 private Mockery context = new JUnit4Mockery();
3 private IHelloService helloService;
4
5 /**
6 * @throws java.lang.Exception
7 */
8 @Before
9 public void setUp() throws Exception {
10 // set up
11 helloService = context.mock(IHelloService.class);
12 }
13
14 /**
15 * Test method for
16 * {@link org.hook.jmock.firstcase.HelloServiceImpl#sayHelloToSomebody(java.lang.String)}.
17 */
18 @Test
19 public void testSayHelloToSomebody() {
20 final String message = "HELLO,alex!";
21 final String name = "alex";
22 // expectations
23 context.checking(new Expectations() {
24 {
25 one(helloService).sayHelloToSomebody(name);
26 will(returnValue(message));
27 }
28 });
29 // execute
30 String result = helloService.sayHelloToSomebody(name);
31 // verify
32 context.assertIsSatisfied();
33 assertSame(result, message);
34 }
35 }
2.测试类:
HelloService:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1 public class HelloService {
2
3 /**
4 * @param name
5 * @return hello message
6 */
7 public String sayHelloToSomebody(String name) {
8 return "HELLO," + name + "!";
9 }
10 }
Test Case:2
3 /**
4 * @param name
5 * @return hello message
6 */
7 public String sayHelloToSomebody(String name) {
8 return "HELLO," + name + "!";
9 }
10 }
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1 public class HelloServiceTest extends TestCase {
2 private Mockery context;
3 private HelloService helloService;
4
5 /**
6 * @throws java.lang.Exception
7 */
8 @Before
9 public void setUp() throws Exception {
10 context = new JUnit4Mockery();
11 // 声明针对类进行mock,针对接口则会采用动态代理,不需要声明
12 context.setImposteriser(ClassImposteriser.INSTANCE);
13 helloService = context.mock(HelloService.class);
14 }
15
16 /**
17 * Test method for
18 * {@link org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String)}.
19 */
20 @Test
21 public void testSayHelloToSomebody() {
22 final String message = "HELLO,vivian!";
23 final String name = "vivian";
24 // expectations
25 context.checking(new Expectations() {
26 {
27 one(helloService).sayHelloToSomebody(name);
28 will(returnValue(message));
29 }
30 });
31 // execute
32 String result = helloService.sayHelloToSomebody(name);
33 // verify
34 context.assertIsSatisfied();
35 assertSame(result, message);
36 }
37 }
OK,跑下测试,green bar again...2 private Mockery context;
3 private HelloService helloService;
4
5 /**
6 * @throws java.lang.Exception
7 */
8 @Before
9 public void setUp() throws Exception {
10 context = new JUnit4Mockery();
11 // 声明针对类进行mock,针对接口则会采用动态代理,不需要声明
12 context.setImposteriser(ClassImposteriser.INSTANCE);
13 helloService = context.mock(HelloService.class);
14 }
15
16 /**
17 * Test method for
18 * {@link org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String)}.
19 */
20 @Test
21 public void testSayHelloToSomebody() {
22 final String message = "HELLO,vivian!";
23 final String name = "vivian";
24 // expectations
25 context.checking(new Expectations() {
26 {
27 one(helloService).sayHelloToSomebody(name);
28 will(returnValue(message));
29 }
30 });
31 // execute
32 String result = helloService.sayHelloToSomebody(name);
33 // verify
34 context.assertIsSatisfied();
35 assertSame(result, message);
36 }
37 }
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
发表评论
-
JUnit4概述 (非常好的文章for java unit test)
2012-10-23 15:31 1016JUnit4是JUnit框架有史以来的最大改进,其主要目标 ... -
Weblogic remote debugger java
2012-09-06 15:15 708window version 1, add b ... -
Base code of using Junit and Jmock write unit test case
2012-09-06 15:09 860mock class and mock interface ... -
jMock Cookbook 中文版一
2012-02-07 10:50 1230入门 定义期望 模拟 ... -
[1.18开始更新了北风网&风中叶录制的JUNIT详解-Spring.MVC3.0.WEB框架系列---北风网讲师深入讲解Java编程陷阱及面试指导系列课程(共
2012-02-06 00:05 2[1.18开始更新了北风网&风中叶录制的JUNIT详解 ... -
(来自IBM) JUnit4 与 JMock 之双剑合璧
2012-02-03 15:29 1184引言 单元测试可以保证代码的质量,最大程度降低修复系统 bu ... -
maven+jmock
2012-02-03 13:50 943Maven + jmock + dependency +rep ... -
Junit使用指南及作业规范.pdf (Open-Open)
2012-02-05 23:09 831Junit使用指南及作业规范.pdf http:// ... -
junit示例
2012-02-05 23:10 893junit示例 1.TestCase最 ... -
(来自IBM)单元测试利器 JUnit 4
2012-02-03 15:27 802引言 毋庸置疑,程序 ... -
JUnit三分钟教程 ---- 快速起步
2012-02-05 23:09 831JUnit三分钟教程 ---- 快速起步 JUnit是个好东西 ... -
Junit
2012-02-02 11:16 926第一,junit使用很方便 ... -
Jmock Jar Package
2012-02-02 14:02 796Jmock -
Growing Object-Oriented Software Guided by Tests
2012-02-02 14:02 994About the Book <script>& ... -
使用MOCK对象进行单元测试
2012-02-01 17:34 1070测试之道 1.出了什么问题? 单元测试的 ... -
教你如何使用JMock来实现孤立测试
2012-02-01 17:03 801JMock是帮助创建mock对 ... -
jmock
2012-02-01 15:19 1462一、介绍 JMock是一个使用模拟对象机制测试Java ...
相关推荐
jMock 2: Java 5 and above. Stable: 2.5.1 Binary JARs (includes the source to let IDEs provide context-sensitive help) Javadocs Unstable: 2.6.0-RC2 Binary JARs (includes the source to let IDEs ...
在 developerWorks 上面有一些关于 JUnit4 的文章,也有关于 JMock 的文章,但是结合这两项技术的文章基本上没有。本文就基于这两种技术,对单元测试做一个全面的阐述。本文的目的不光是介绍层面的文字,更侧重作者...
JMock2.5.1和EasyMock3.0以及一些JUnit单元测试所需的jar,让你使用JMock和EasyMock时不用到处找相关的jar。
Junit 基础教程与学习 Junit 是一个流行的 Java 单元测试框架,旨在帮助开发人员编写可靠、稳定、快速的代码。本文档旨在为初学者提供一个初步的了解 Junit 的认识,帮助他们快速入门轻松容易的进入到实际的项目中...
jmock可以模拟对象,常用于单元测试,与junit配合使用 jmock-2.5.1-javadoc
jMock 模板 context : 声明一个Mockery实例。 expectations :声明用于指定期望的上下文。 mock :声明一个模拟实例。 rv :声明一个模拟的返回值。 Eclipse 首选项 Eclipse最爱 紧凑可读的测试代码使用了许多静态...
jmock2.5.1.zip
用于项目测试,JMOCK,可以与JUNIT搭配进行测试
JMock is a library that supports test-driven development1 of Java2 code with mock objects3. Mock objects help you design and test the interactions between the objects in your programs.
Jmock2.6 jar包
jmock jar和源码
Maven-Central / jmock / jmock-cglib 1.2.0 Maven-Central / org.apache.activemq / activemq-ra 5.2.05.1.0 Maven-Central / org.apache.maven.shared / maven-dependency-tree 1.21.1 Maven-Central / org.apache...
jmock学习文档,模拟对象技术,jMock 可以和既有的其他测试框架,如JUnit,很好地整合在一起,共同使用。
JMOCK 帮助 网页 文档 在网上找的一些bolg里的文档 JMOCK 帮助 网页 文档 在网上找的一些bolg里的文档
Junit开源测试框架 -Junit -Jmock -selenium -Apache JMeter
jMock利用mock objects思想来对Java code进行测试。jMock具有以下特点:容易扩展,让你快速简单地定义mock objects,因此不必打破程序间的关联,让你定义灵活的超越对象之间交互作用而带来测试局限,减少你测试地脆弱...
Jmock学习手册,让你更快学习Jmock。。
Jmock Mock 没有接口的类 很方便使用
jmock-1.0.1.jar jmock-1.0.1.jar
jmock2.5基本教程目录第0章 概述第1章 jmock初体验第2章 期望第3章 返回值第4章 参数匹配第5章 指定方法调用次数第6章 指定执行序列第7章 状态机第0章 概述