首页 > 解决方案 > Mockito 在 java 8 中运行,但在 java 11 中给出未完成的存根错误

问题描述

我在编写单元测试时使用 mockito 来模拟一些方法。它在 java 8 中运行良好,但是当我使用 java 11 时,它会出现以下错误:

这是我正在使用的模拟依赖项:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.10.19</version>
  <scope>test</scope>
</dependency>

这是示例单元测试:

public class ClientTest {

private OtherClient otherClient;

@Mock
private OkHttpClient client;

@Mock
private Call call;

@Before
public void setUp() throws IOException {
    MockitoAnnotations.initMocks(this);
    otherClient = new OtherClient(client, new ObjectMapper(), "http://localhost:8080");
    Mockito.when(client.newCall(Mockito.any())).thenReturn(call);
}

@Test
public void testUploadSuccess() throws IOException {

    Response.Builder builder = new Response.Builder();
    builder.code(200);
    builder.request(new Request.Builder().url("http://localhost:8080").build());
    builder.protocol(Protocol.HTTP_1_1);
    builder.body(ResponseBody.create(MediaType.parse("application/json"), "{}"));
    Mockito.when(call.execute()).thenReturn(builder.build());
    File temp = File.createTempFile("upload", "test.pdf");
    try {
        otherClient.upload("test", temp,
                FileUploadRequest.builder().build(),
                ImmutableMap.of("Authorization", ""));
    } catch (UploadException e) {
        Assert.fail("Failed to Upload");
    } finally {
        FileUtils.deleteQuietly(temp);
    }
}
}

请帮忙。

标签: javamockito

解决方案


推荐阅读