junit模拟泛型类作为方法参数

zysjyyx4  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(448)

我正在编写单元测试,并通过junit框架验证其中的方法调用。我实现的方法在我的代码库中说明如下:

AtlasBaseClient.API api = new AtlasBaseClient.API("api/atlas/v2/types/typedefs","PUT",Response.Status.OK);

1.     client.callAPI(api, (Class<?>) null, (Object) objectMapper.writeValueAsString(type), null);
2.     client.callAPI(api, (Class<?>) null, object, null);

以上方法的库声明为:

public <T> T callAPI(AtlasBaseClient.API api, Class<T> responseType, Object requestObject, String... params) throws AtlasServiceException {
    return this.callAPIWithResource(api, this.getResource(api, params), requestObject, responseType);
  }

现在,我正在编写单元测试,并希望验证是否调用了这些方法,以及我正在尝试的代码段的外观

verify(client, times(1)).callAPI(any(AtlasBaseClient.API.class), (Class<?>) null, any(Object.class), any());

但按照junit框架,如果其中一个参数通过matcher,那么其他参数也应该通过matcher,这使得测试失败。

qeeaahzv

qeeaahzv1#

verify(client, times(2)).callAPI(any(AtlasBaseClient.API.class), Mockito.<Class>isNull(), any(Object.class), any());
n9vozmp4

n9vozmp42#

如果你用匹配器 verify 测试所有参数都需要匹配,这是什么 InvalidUseOfMatchersException 手段。
您正在显式测试的第二个参数 null ,应该是 Mockito.isNull() 或者类似的东西。

verify(client, times(1)).callAPI(any(AtlasBaseClient.API.class), isNull(), any(Object.class), any());

相关问题