mockito 错误:组织.模拟.异常.误用.缺少方法调用异常

8zzbczxx  于 2023-02-08  发布在  其他
关注(0)|答案(1)|浏览(248)

我在将java升级到17、将mockito升级到3.7.7、将junit4升级到junit 5时遇到以下错误,而且我正在使用这些powermock依赖项。
'org.powermock:powermock-module-junit4:2.0.7', 'org.powermock:powermock-core:2.0.7', 'org.powermock:powermock-api-mockito2:2.0.7'

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

    at app//com.expedia.risk.clm.web.exception.handler.RestExceptionMappingHandlerTest.shouldHandleMethodArgumentNotValid(RestExceptionMappingHandlerTest.java:118)
    at app//org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at app//org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at app//org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at app//org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at app//org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at app//org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:54)
    at app//org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at app//org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at app//org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at app//org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at app//org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at app//org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at app//org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at app//org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at app//org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at app//org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at app//org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at app//org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at app//org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:99)
    at app//org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:105)
    at app//org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:40)

我的测试用例代码是这样的。有人能帮忙解决这个问题吗?

@Test
    public void shouldHandleMethodArgumentNotValid() {
        final Set<InputError> testInputErrors = Collections.singleton(new InputError("test", "test"));
        final String expectedMessage = String.format("Invalid input. Details: %s", testInputErrors.toString());
        final MethodArgumentNotValidException testException = mock(MethodArgumentNotValidException.class);
        final BindingResult testBindingResult = mock(BindingResult.class);
        final List<ObjectError> testErrors = Collections.singletonList(new ObjectError("test", "test"));
        when(testException.getBindingResult()).thenReturn(testBindingResult);
        when(testBindingResult.getAllErrors()).thenReturn(testErrors);

        final ResponseEntity<ClmErrorResponse> actualResponse = handler.handleMethodArgumentNotValid(testException);

        assertThat(actualResponse.getStatusCode(), is(HttpStatus.BAD_REQUEST));
        final ClmErrorResponse errorResponse = actualResponse.getBody();
        assertThat(errorResponse.getCode(), is("39071"));
        assertThat(errorResponse.getMessage(), is(expectedMessage));
        assertThat(errorResponse.getInputErrors(), is(testInputErrors));
        verify(testException).getBindingResult();
        verify(testBindingResult).getAllErrors();
        verifyNoMoreInteractions(testBindingResult);
    }

尝试升级和降级mockito和powermock的版本。

pkln4tw6

pkln4tw61#

你试图模仿一个final方法:
此外,出现此错误的原因可能是:
1.您将以下任何一项存根:final/private/equals()/hashCode()方法。
BindException.getBindingResult()为:

public class BindException .. {

    ...

    public final BindingResult getBindingResult() {
        return this.bindingResult;
    }
}

不看代码很难判断,但你可能没有正确使用PowerMock。下面的答案给出了一个如何模拟final方法的例子:Can Powermockito mock final method in non-final concrete class?

相关问题