【发布时间】:2014-03-27 11:46:14
【问题描述】:
我目前正在尝试做一个垄断游戏的模拟测试课。
我们已经获得了一些关于如何设置它的说明,但也许我们只是误解了 JMock 的工作原理。
我们有使用 takeTurn() 的 Player 类。我们有Die、Board和Piece,这些都是支持嘲讽的接口。然后我们有 Square 类,它不保存任何值,但只是用来表示一个 Square。也许我们应该让它成为一个接口,因为它什么都没有,但我不知道。
无论我们做什么,测试总是失败。我试图省略部分,这样我们也只做一个期望,但没有运气。我们只是完全误解了 JMock 吗?
这是我们的 Player 类:
public class Player {
Die d;
Piece p;
Board b;
void takeTurn(Die d, Piece p, Board b) {
this.d = d;
this.p = p;
this.b = b;
int i = d.roll();
int v = d.roll();
Square newLoc = b.getSquare(p.getLocation(), i + v);
p.setLocation(newLoc);
}
}
这是我们的 PlayerTest 类:
public class PlayerTest extends TestCase {
Mockery context = new Mockery();
public void testTakeTurn() {
final Board b = context.mock(Board.class);
final Piece p = context.mock(Piece.class);
final Die d = context.mock(Die.class);
Player pl = new Player();
context.checking(new Expectations() {{
exactly(2).of (d).roll();
}});
context.checking(new Expectations() {{
oneOf (p).getLocation();
}});
final int diceroll = 5;
context.checking(new Expectations() {{
oneOf (b).getSquare(p.getLocation(), diceroll);
}});
final Square newLoc = new Square();
context.checking(new Expectations() {{
oneOf (p).setLocation(newLoc);
}});
pl.takeTurn(d,p,b);
context.assertIsSatisfied();
}
}
【问题讨论】:
-
查看 JMock 失败消息会很有帮助。