我是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);
}
}
这是我的Mock对象测试:
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)
答案 0 :(得分:9)
您可能必须在调用facade方法之前而不是之后定义您的期望。
答案 1 :(得分:1)
另外,你似乎在混合使用JUnit 3和4.我建议你选择一个。 @RunWith(JMock.class)是4,否则你需要JMock的JUnit 3集成附带的TestCase类。