あなたはverify
メソッド呼び出しでパラメータの予想されるタイプを指定する場合、これは動作します:
import static org.junit.Assert.fail;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
public class MockitoNotNullTest {
private static class Dependency1 {
}
private static class Dependency2 {
public void doesNotAcceptNull(Dependency1 d) {
}
}
private static class ClassUnderTest {
private final Dependency2 d2;
public ClassUnderTest(Dependency2 d2) {
this.d2 = d2;
}
public void passesNull() {
d2.doesNotAcceptNull(null);
}
}
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
Dependency2 d2;
@Test
public void passesNull_callsD1WithNull_verifyFails() {
ClassUnderTest classUnderTest = new ClassUnderTest(d2);
classUnderTest.passesNull();
// fails as expected
Mockito.verify(d2).doesNotAcceptNull(Mockito.any(Dependency1.class));
}
}
をし、結果は次のとおりです。
Argument(s) are different! Wanted:
d2.doesNotAcceptNull(
<any stackoverflow.MockitoNotNullTest.Dependency1>
);
-> at stackoverflow.MockitoNotNullTest.passesNull_callsD1WithNull_verifyFails(MockitoNotNullTest.java:48)
Actual invocation has different arguments:
d2.doesNotAcceptNull(
null
);
-> at stackoverflow.MockitoNotNullTest$ClassUnderTest.passesNull(MockitoNotNullTest.java:32)
// ...
それはMockito 1.10.19です。 – user3681970