1

テストコードは次のとおりです。

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().

私は何が間違っているのですか?

4

1 に答える 1

2

私の悪い...

JMockitAPIとMockitoのAPIを混同したように見えます。

Lockインターフェイスをモックするために必要なのは次のとおりです。

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

    }
于 2012-11-25T16:00:24.970 に答える