java—在mockito中捕获静态方法调用的参数

a0zr77ik  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(486)

我正在尝试使用mockito在androidstudio中对方法调用进行单元测试。是这样的:

public static class DeviceMessageHandler extends Handler {
    public DeviceMessageHandler(Looper looper) {
        super(looper);
}

@Override
    public void handleMessage(Message msg) {

        try {
            switch (msg.what) {
                default:
                    String message = (String) msg.obj;

                    DeviceRequest deviceRequest = new Gson().fromJson(message, DeviceRequest.class);
                    String requestType = deviceRequest.getKey();
                    if (DeviceMessage.SOME_ACTION.equals(requestType)) {
                        sendResponseWithMessage(execId, COMMAND_STATE, "Not accepted");
                    }
                }
            }
        }
}

public static void sendResponseWithMessage(String execId, String commandState, String reasonIfAny) {
//  do some work
} catch (Exception e) {
        e.printStackTrace();
    }
}

所以在这里我可以打电话给sendresponsewithmessage。我想检查调用此方法时传递的参数。问题是它是一个静态方法,我不知道如何捕获传递给静态方法的参数。我试过使用powermock,但由于这是一个android项目,我不能使用powermock。有什么方法可以用期望值Assert传递给方法调用的参数吗?

qlckcl4x

qlckcl4x1#

对于我来说,在android中运行powermock很管用:

testImplementation 'org.mockito:mockito-core:3.7.7'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
testImplementation group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.9.0'
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'

相关问题