2

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)
Mike
  • 19,267
  • 11
  • 56
  • 72

2 Answers2

9

You might have to define your expectations before you call the facade method instead of after.

rsp
  • 23,135
  • 6
  • 55
  • 69
1

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.

Steve Freeman
  • 2,707
  • 19
  • 14