java - JMockit:模拟接口(interface)时为 "Misplaced argument matcher detected here"

标签 java unit-testing mockito jmockit

以下是测试代码:

package a.b.c.concurrent.locks;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.when;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

import mockit.Mocked;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestLockConcludesProperly {

    private static LockProxy lockProxy;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout()));
    }

    @Test
    public void testAquireLockInFirstIteration(@Mocked Lock mockLock) throws Exception {

        when(mockLock.tryLock(anyLong(), any(TimeUnit.class))).thenReturn(true);

        lockProxy = new LockProxy(mockLock, 2, TimeUnit.MILLISECONDS);

        lockProxy.lock();

    }
}

我得到的错误但无法弄清楚,尽管这可能是我的一些配置错误,但它是:

Tests in error: 
  testAquireLockInFirstIteration(a.b.c.concurrent.locks.TestLockConcludesProperly): 
Misplaced argument matcher detected here:

-> at a.b.c.concurrent.locks.TestLockConcludesProperly.testAquireLockInFirstIteration(TestLockConcludesProperly.java:34)
-> at a.b.c.concurrent.locks.TestLockConcludesProperly.testAquireLockInFirstIteration(TestLockConcludesProperly.java:34)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().

我做错了什么?

最佳答案

我的错...

看起来好像我混淆了 JMockit API 和 Mockito 的 API。

为了模拟 Lock 接口(interface),我需要做的就是:

    @Test
    public void testAquireLockInFirstIteration() throws Exception {

        Lock mockLock = Mockito.mock(Lock.class);
        when(mockLock.tryLock(anyLong(), any(TimeUnit.class))).thenReturn(true);

        lockProxy = new LockProxy(mockLock, 2, TimeUnit.MILLISECONDS);

        lockProxy.lock();

    }

关于java - JMockit:模拟接口(interface)时为 "Misplaced argument matcher detected here",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552300/

相关文章:

java - JPanel 和 Frame 之间的默认彩色间距

javascript - 在 JavaScript Protractor 端到端测试中模拟 HTTP 后端

java - Mockito 方法调用链

java - NoClassDefFound错误: org/hamcrest/Matchers using PowerMock-OSGi

java - 解释阶乘方法中的递归

java - eclipse中Ctrl Alt H和f4有什么区别

reactjs - Jest 错误 - 未实现 : navigation (except hash changes) when click event is triggered on an anchor element

java - 如何让 Mockito 模拟按顺序执行不同的操作?

java - conwert 对象,其中包含 utf-8 的字符串到具有正确编码的字符串

c - 使用 Cmockery 验证函数指针是否相等