使用Mockito Matchers.any()和android.support.annotation.IntDef自定义注释

HOD*_*HOD 6 junit android unit-testing mockito

我正在尝试编写一个Junit测试,它将验证是否调用了以下方法:

public long executeRequest(@RequestCodes.Code.RequestAnnotation int requestCode, Object requestInformation, RequestListener requestListener) {

    boolean success = false;

    ... do stuff ...

    return success ? 1L : -1L;

}
Run Code Online (Sandbox Code Playgroud)

在测试中使用:

Mockito.when(mockedRequest.executeRequest(Matchers.any(RequestCodes.Code.RequestAnnotation.class), Matchers.any(RequestWrapper.class), Matchers.any(RequestListener.class))).thenReturn(1L);
Run Code Online (Sandbox Code Playgroud)

RequestCodes.Code.RequestAnnotation类是一个基本的indef接口,使用int来标识使用开关进行的调用.非常喜欢这个.

Matchers.any(RequestCodes.Code.RequestAnnotation.class)不会在这里工作,我都试过Matchers.any(),Matchers.anyInt(),Matchers.isA(RequestCodes.Code.RequestAnnotation.getClass())(以及别的来考虑),但没有成功.

任何建议将不胜感激.

Abd*_*tou 2

现在,您可以使用@SuppressWarnings("WrongConstant")此特定测试来抑制此错误。它工作正常,并保持您的生产清洁。