Цепочка PowerMockito не работает надежно для класса Spied - PullRequest
0 голосов
/ 01 мая 2018

У меня есть тест, в котором тестируемая система вызывает статический метод. Статический метод принадлежит к конечному классу (это служебный класс). Тестовый код выглядит следующим образом:

@Test
public void test() throws Exception {
  SUT sut = PowerMockito.spy(new SUT());
  PowerMockito.spy(A.class);
  stub(method(A.class, "methodOne")).toReturn("CONST_STRING");
  PowerMockito.when(A.methodTwo(anyLong(), anyLong()))
                .thenReturn(false, true, false); //The problem!
  sut.method("arg1");
  assertThat(//some assert);
}

В следующей строке я получаю периодические ошибки. Это проходит иногда и иногда, это терпит неудачу.

PowerMockito.when(A.methodTwo(anyLong(), anyLong()))
                    .thenReturn(false, true, false);

При сбое ниже отображается сообщение об ошибке:

[junit] Misplaced argument matcher detected here:
    [junit]
    [junit] -> at com.project.SUTTest.test(SUTTest.java:40)
    [junit] -> at com.project.SUTTest.test(SUTTest.java:40)
    [junit]
    [junit] You cannot use argument matchers outside of verification or stubbing.
    [junit] Examples of correct usage of argument matchers:
    [junit]     when(mock.get(anyInt())).thenReturn(null);
    [junit]     doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    [junit]     verify(mock).someMethod(contains("foo"))
    [junit]
    [junit] Also, this error might show up because you use argument matchers with methods that cannot be mocked.
    [junit] Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
    [junit] Mocking methods declared on non-public parent classes is not supported.
    [junit]
    [junit] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
    [junit] Misplaced argument matcher detected here:
    [junit]
    [junit] -> at com.project.SUTTest.test(SUTTest.java:40)
    [junit] -> at com.project.SUTTest.test(SUTTest.java:40)
    [junit]
    [junit] You cannot use argument matchers outside of verification or stubbing.
    [junit] Examples of correct usage of argument matchers:
    [junit]     when(mock.get(anyInt())).thenReturn(null);
    [junit]     doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    [junit]     verify(mock).someMethod(contains("foo"))
    [junit]
    [junit] Also, this error might show up because you use argument matchers with methods that cannot be mocked.
    [junit] Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
    [junit] Mocking methods declared on non-public parent classes is not supported.

Моя версия PowerMockito - 1.6.3 Что происходит? Почему сбой прерывистый? Что вызывает это ненадежное поведение?

PowerMockito не работает надежно для класса Spied

...