2

我是 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)
4

2 回答 2

9

您可能必须在调用外观方法之前而不是之后定义您的期望。

于 2011-03-13T15:28:44.360 回答
1

此外,您似乎正在混合使用 JUnit 3 和 4。我建议您只使用一个。@RunWith(JMock.class) 是 4 的,否则你需要 JMock 的 JUnit 3 集成附带的 TestCase 类。

于 2011-03-14T20:14:33.160 回答