jmock使用总结

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使用总结)