jmock是写单元测试时需要生成mock对象时很好的辅助库。
软件地址: http://www.jmock.org
本文是我今天摸索使用jmock(v2.4)的总结。不是初学指南,当你入门了,我想可以作为简单手册。是原版文档补充。
一般使用模式:
生成Mockery对象,可以全局共享
Java代码
Mockery context = new JUnit4Mockery() {
{
//声明针对类mock(采用cglib生成子类),final方法无法mock,针对接口则会采用动态代理,不需要声明下面的代码
setImposteriser(ClassImposteriser.INSTANCE);
}
};
Mockery context = new JUnit4Mockery() {
{
//声明针对类mock(采用cglib生成子类),final方法无法mock,针对接口则会采用动态代理,不需要声明下面的代码
setImposteriser(ClassImposteriser.INSTANCE);
}
};
例子
Java代码
final UserDAO dao = context.mock(UserDAO.class);
final Integer id1 = 1;
final User user1 = new User();
user1.setId(1);
user1.setUserId(1);
user1.setName("1");
final Integer id2 = 2;
final User user2 = new User();
user2.setId(2);
user2.setUserId(2);
user2.setName("2");
context.checking(new Expectations(){
{
//one表示调用一次。
one(dao).find(id1);will(returnValue(user1));
//再用另一参数做一次mock
one(dao).find(id2);will(returnValue(user2));
//可以忽略mock对象
//ignoring(dao);
}
});
final UserDAO dao = context.mock(UserDAO.class);
final Integer id1 = 1;
final User user1 = new User();
user1.setId(1);
user1.setUserId(1);
user1.setName("1");
final Integer id2 = 2;
final User user2 = new User();
user2.setId(2);
user2.setUserId(2);
user2.setName("2");
context.checking(new Expectations(){
{
//one表示调用一次。
one(dao).find(id1);will(returnValue(user1));
//再用另一参数做一次mock
one(dao).find(id2);will(returnValue(user2));
//可以忽略mock对象
//ignoring(dao);
}
});
context.checking(new Expectations(){中的语法总结
1.
Java代码
context.checking(new Expectations(){
{
//另一种连续调用: 同个方法同样参数,调用多次返回值各不同,声明了调用次序
atLeast(1).of(dao).find(id1);
will(onConsecutiveCalls(
returnValue(user1),
returnValue(user2)));
}
});
context.checking(new Expectations(){
{
//另一种连续调用: 同个方法同样参数,调用多次返回值各不同,声明了调用次序
atLeast(1).of(dao).find(id1);
will(onConsecutiveCalls(
returnValue(user1),
returnValue(user2)));
}
});
2.
Java代码
context.checking(new Expectations(){
{
allowing(dao).find(id);will(throwException(new FindException()));//也可以throw RuntimeException
}
});
context.checking(new Expectations(){
{
allowing(dao).find(id);will(throwException(new FindException()));//也可以throw RuntimeException
}
});
3.
Java代码
context.checking(new Expectations(){
{
//allowing不限定调用次数
allowing(dao).find(id);will(returnValue(user));
never(dao).findByUserId(userId);
//下面两行是从jmock官网copy下来的,可以为相同的方法用不同的参数mock多次
//one(calculator).load("x"); will(returnValue(10));
//never(calculator).load("y");
}
});
context.checking(new Expectations(){
{
//allowing不限定调用次数
allowing(dao).find(id);will(returnValue(user));
never(dao).findByUserId(userId);
//下面两行是从jmock官网copy下来的,可以为相同的方法用不同的参数mock多次
//one(calculator).load("x"); will(returnValue(10));
//never(calculator).load("y");
}
});
4.
Java代码
context.checking(new Expectations(){
{
//可以在mock方法时做一些参数操作
allowing(srv).findUser(with(lessThan(10)));will(returnValue(user1));
allowing(srv).findUser(with(greaterThan(10)));will(returnValue(user2));
}
});
context.checking(new Expectations(){
{
//可以在mock方法时做一些参数操作
allowing(srv).findUser(with(lessThan(10)));will(returnValue(user1));
allowing(srv).findUser(with(greaterThan(10)));will(returnValue(user2));
}
});
5.
Java代码
context.checking(new Expectations(){
{
//inSequence(sequence),会把声明的方法放进序列,调用次序必须符合声明次序
one(dao).find(id1);will(returnValue(user1));inSequence(sequence);
one(dao).find(id2);will(returnValue(user2));inSequence(sequence);
}
});
//调用次序必须符合声明次序
assertEquals(user1,dao.find(id1));
assertEquals(user2,dao.find(id2));
分享到:
相关推荐
这个是几篇关于如何使用JMOCK对java代码测试,如果实在不清楚,也可咨询 445054051
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 ...可以直接使用
jmock2.5.1.zip
jmock可以模拟对象,常用于单元测试,与junit配合使用 jmock-2.5.1-javadoc
详细介绍了Jmock的使用方法。如何编写自己的单元测试代码。
Jmock2.6 jar包
Jmock Mock 没有接口的类 很方便使用
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 jar和源码
JMOCK 帮助 网页 文档 在网上找的一些bolg里的文档 JMOCK 帮助 网页 文档 在网上找的一些bolg里的文档
JMock2.5.1和EasyMock3.0以及一些JUnit单元测试所需的jar,让你使用JMock和EasyMock时不用到处找相关的jar。
Jmock学习手册,让你更快学习Jmock。。
jmock-1.0.1.jar jmock-1.0.1.jar
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.
NULL 博文链接:https://tristan-s.iteye.com/blog/1750265
jmock-1.2.0-jars.rar
jmock学习文档,模拟对象技术,jMock 可以和既有的其他测试框架,如JUnit,很好地整合在一起,共同使用。
jmock2.5基本教程目录第0章 概述第1章 jmock初体验第2章 期望第3章 返回值第4章 参数匹配第5章 指定方法调用次数第6章 指定执行序列第7章 状态机第0章 概述
jMock利用mock objects思想来对Java code进行测试。jMock具有以下特点:容易扩展,让你快速简单地定义mock objects,因此不必打破程序间的关联,让你定义灵活的超越对象之间交互作用而带来测试局限,减少你测试地脆弱...
NULL 博文链接:https://alicsd.iteye.com/blog/845023