jMock 期望未指定
我是 jMock 的新手,所以我正在尝试一个简单的例子。但我不明白为什么它不起作用。这是我正在测试的类:
package com.application;
import com.domain.Coordinate;
import com.domain.Playable;
public class ChessFacade {
private final Playable board;
public ChessFacade(Playable aBoard) {
board = aBoard;
}
public void showPotentialMoves(Coordinate aCoordinate) {
board.getTileOccupancy(aCoordinate);
}
}
这是我的模拟对象测试:
package unit.application;
import application.ChessFacade;
import com.domain.Coordinate;
import com.domain.Playable;
import junit.framework.TestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.runner.RunWith;
@RunWith(JMock.class)
public class ChessFacadeTest extends TestCase {
public void testFacadeGetsPotentialMovesFromBoard() {
Mockery context = new JUnit4Mockery();
final Playable mockBoard = context.mock(Playable.class);
ChessFacade facade = new ChessFacade(mockBoard);
final Coordinate locationToShow = new Coordinate(0, 0);
facade.showPotentialMoves(locationToShow);
context.checking(new Expectations() {{
oneOf(mockBoard).getTileOccupancy(locationToShow);
}});
context.assertIsSatisfied();
}
}
我收到的错误是:
Testcase: testFacadeGetsPotentialMovesFromBoard(unit.application.ChessFacadeTest): Caused an ERROR
unexpected invocation: playable.getTileOccupancy(<Coordinate{row=0column=0}>)
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
java.lang.AssertionError: unexpected invocation: playable.getTileOccupancy(<Coordinate{row=0column=0}>)
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
at org.jmock.Mockery.dispatch(Mockery.java:218)
at org.jmock.Mockery.access$000(Mockery.java:43)
at org.jmock.Mockery$MockObject.invoke(Mockery.java:258)
at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
at $Proxy0.getTileOccupancy(Unknown Source)
at application.ChessFacade.showPotentialMoves(ChessFacade.java:15)
at unit.application.ChessFacadeTest.testFacadeGetsPotentialMovesFromBoard(ChessFacadeTest.java:22)
I'm new to jMock, so I'm trying it out on a simple example. I can't figure out why it's not working, though. Here's the class that I'm testing:
package com.application;
import com.domain.Coordinate;
import com.domain.Playable;
public class ChessFacade {
private final Playable board;
public ChessFacade(Playable aBoard) {
board = aBoard;
}
public void showPotentialMoves(Coordinate aCoordinate) {
board.getTileOccupancy(aCoordinate);
}
}
And here's my Mock Object test:
package unit.application;
import application.ChessFacade;
import com.domain.Coordinate;
import com.domain.Playable;
import junit.framework.TestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.runner.RunWith;
@RunWith(JMock.class)
public class ChessFacadeTest extends TestCase {
public void testFacadeGetsPotentialMovesFromBoard() {
Mockery context = new JUnit4Mockery();
final Playable mockBoard = context.mock(Playable.class);
ChessFacade facade = new ChessFacade(mockBoard);
final Coordinate locationToShow = new Coordinate(0, 0);
facade.showPotentialMoves(locationToShow);
context.checking(new Expectations() {{
oneOf(mockBoard).getTileOccupancy(locationToShow);
}});
context.assertIsSatisfied();
}
}
The error I'm receiving is:
Testcase: testFacadeGetsPotentialMovesFromBoard(unit.application.ChessFacadeTest): Caused an ERROR
unexpected invocation: playable.getTileOccupancy(<Coordinate{row=0column=0}>)
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
java.lang.AssertionError: unexpected invocation: playable.getTileOccupancy(<Coordinate{row=0column=0}>)
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
at org.jmock.Mockery.dispatch(Mockery.java:218)
at org.jmock.Mockery.access$000(Mockery.java:43)
at org.jmock.Mockery$MockObject.invoke(Mockery.java:258)
at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
at $Proxy0.getTileOccupancy(Unknown Source)
at application.ChessFacade.showPotentialMoves(ChessFacade.java:15)
at unit.application.ChessFacadeTest.testFacadeGetsPotentialMovesFromBoard(ChessFacadeTest.java:22)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能必须在调用外观方法之前而不是之后定义您的期望。
You might have to define your expectations before you call the facade method instead of after.
另外,您似乎正在混合使用 JUnit 3 和 4。我建议您只选择其中之一。 @RunWith(JMock.class) 适用于 4,否则您需要 JMock 的 JUnit 3 集成附带的 TestCase 类。
Also, you appear to be mixing JUnit 3 and 4. I suggest you just go with one. The @RunWith(JMock.class) is for 4, otherwise you need to the TestCase class that comes with JMock's JUnit 3 integration.
我尝试移动代码(a)创建模拟和(b)对模拟设置期望,如上面的答案以及 其他发布没有运气。
最终我从 org.jmock.Mockery 迁移到普通的旧 org.mockito.Mockito.mock 并成功了。
I tried moving around code for (a) creating mocks and (b) setting expectations on mocks as called out in above answers as well as this other post without luck.
Eventually I moved from
org.jmock.Mockery
to just plain oldorg.mockito.Mockito.mock
and that worked.