java - Mockito 在 java 8 中运行,但在 java 11 中给出未完成的存根错误
问题描述
我在编写单元测试时使用 mockito 来模拟一些方法。它在 java 8 中运行良好,但是当我使用 java 11 时,它会出现以下错误:
- java.lang.IllegalStateException: 消息 == null
- org.mockito.exceptions.misusing.UnfinishedStubbingException:此处检测到未完成的存根:
这是我正在使用的模拟依赖项:
<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);
}
}
}
请帮忙。
解决方案
推荐阅读
- sql - 如何使用 SQL 计算累积总数
- hibernate - 添加具有多对多关系的实体,具有相关实体的代理
- sql-server - 无法在没有 LDF 日志文件的情况下附加 MDF 数据库
- c# - 在数组中存储串行读取字节
- sql - 将 varchar 转换为十进制棒球平均值
- r - Reulerr 包 - 显示错误的 euler 图
- xml - 来自 xml 输入的 Xslt 格式编号
- swift - Firebase 将 URL 上传到实时数据库
- javascript - React - 使用 Object.keys.map 迭代时检查字符串中的空格
- python - 如何根据多个条件顺序评估Dataframe中的列