0

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

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

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

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();
    }
}

和我正在测试的可运行文件:

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;
        }
    }
}
4

1 回答 1

1

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

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

于 2012-01-24T09:55:05.993 回答