JMock触发AssertionError:invokation预期一次,从不调用 - 但它已被调用

时间:2011-12-13 13:43:34

标签: junit jmock

我对使用java编程很新,但我试图直接从单元测试开始,因此也使用了JMock。我已经实现了一些可用的测试用例(使用JMock),但是这个我无法运行。

我做了什么: 我编写了一个测试类,它创建了一个模拟对象,然后我期望一个(使用oneOf)调用。运行单元测试后,它说它失败了(但是日志说不然,因为我打印出我在调用时使用will(returnValue(x))返回的数据。

下一个有趣/奇怪的事情是 - 如果我将oneOf更改为“never”单元测试成功,但它会抛出异常:

Exception in thread "Thread-2" java.lang.AssertionError: unexpected invocation: blockingQueue.take()

期望:   期望永远,永远不要调用:blockingQueue.take();回报 在此之前发生了什么:没事!

这里是代码:

@RunWith(JMock.class)
public class ExecuteGameRunnableTest {
    private Mockery context = new JUnit4Mockery();
    private Thread testObject;
    private BlockingQueue<Game> queueMock;
    private Executor executorMock;

    @SuppressWarnings("unchecked")
    @Before
    public void setUp() {
        queueMock = context.mock(BlockingQueue.class);
        executorMock = context.mock(Executor.class);
        testObject = new Thread(new ExecuteGameRunnable(queueMock, executorMock, true));
    }

    @After
    public void tearDown() {
        queueMock = null;
        executorMock = null;
        testObject = null;
    }

    @Test
    public void testQueueTake() throws InterruptedException {
        final Game game = new Game();
        game.setId(1);
        game.setProcessing(false);
        context.checking(new Expectations() {{
            never(queueMock).take(); will(returnValue(game));
        }});
        testObject.start();
        context.assertIsSatisfied();
    }
}

和我正在测试的runnable:

public class ExecuteGameRunnable implements Runnable {
    private BlockingQueue<Game> queue;
    private Executor executor;
    private Boolean unitTesting = false;
    static Logger logger = Logger.getLogger(ExecuteGameRunnable.class);

    public ExecuteGameRunnable(BlockingQueue<Game> queue, Executor executor) {
        this.queue = queue;
        this.executor = executor;
    }

    public ExecuteGameRunnable (BlockingQueue<Game> queue, Executor executor, Boolean unitTesting) {
        this(queue,executor);
        this.unitTesting = unitTesting;
    }

    public void run() {
        try {
            do {
                if (Thread.interrupted()) throw new InterruptedException();
                Game game = queue.take();
                logger.info("Game "+game.getId()+" taken. Checking if it is processing"); // THIS ONE PRINTS OUT THE GAME ID THAT I RETURN WITH JMOCK-FRAMEWORK
                if (game.isProcessing()) {
                    continue;
                }
                game.updateProcessing(true);

                executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                    }
                });
            } while (!unitTesting);
        } catch (InterruptedException ex) {
            logger.info("Game-Execution-Executor interrupted.");
            return;
        } catch (DataSourceException ex) {
            logger.fatal("Unable to connect to DB whilst executing game: "+id_game,ex);
            return;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

JMock不是线程安全的。它旨在支持单元测试,而不是一个非常小的集成测试。坦率地说,在这种情况下,我会使用真实的BlockingQueue而不是模拟的unitTesting。并且您的生产代码中不应该有{{1}}标记。

还有一件事,你不需要将测试类中的字段设置为null,jUnit会为每个测试刷新实例。