Я пытаюсь перенести тысячи тестов с Mockito 1.x на Mockito 2.x.Некоторые тесты (в настоящее время) помещают общую настройку насмешки в метод @Before, например:
@Before
public void before() {
//...
Mockito.when(something.doStuff(Mockito.<Boolean>any())).thenReturn(result);
//...
}
Тесты терпят неудачу с чем-то вроде:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced or misused argument matcher detected here:
-> at ...SomeClassTest.before(SomeClassTest.java:<line-number>)
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"))
This message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use
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().
Mocking methods declared on non-public parent classes is not supported.
Я не уверен, что фактпричина в том, что это в методе @Before, но похоже, что это так, но я надеюсь на несколько быстрых указателей, прежде чем я начну копать в другом месте ...
Кто-нибудь знает, официально ли подобные вещи официальноподдерживается в методах @Before или нет?