Reflection mockito如何使用methodname和反射进行验证

Reflection mockito如何使用methodname和反射进行验证,reflection,mockito,powermock,verify,powermockito,Reflection,Mockito,Powermock,Verify,Powermockito,我有一个对象的间谍或模拟对象,我想验证是否调用了一个方法 问题是,我在执行时而不是编译时收到methodname 我想做一些类似的事情: SimpleObj mockObject= Mockito.mock(SimpleObj.class); Class myClass = SimpleObj.class; Method meth = myClass.getMethod("getGuid"); Mockito.verify(meth.invoke(mockObject));

我有一个对象的间谍或模拟对象,我想验证是否调用了一个方法 问题是,我在执行时而不是编译时收到methodname

我想做一些类似的事情:

  SimpleObj mockObject= Mockito.mock(SimpleObj.class);
  Class myClass = SimpleObj.class;
  Method meth = myClass.getMethod("getGuid");

  Mockito.verify(meth.invoke(mockObject));
我已经使用

MockingDetails mockingDetails = Mockito.mockingDetails(mockObject);

Collection<Invocation> invocations = mockingDetails.getInvocations();

List<String> methodsCalled = new ArrayList<>();
for (Invocation anInvocation : invocations) {
  methodsCalled.add(anInvocation.getMethod().getName());
}
assertTrue(methodsCalled.contains("getGuid");
MockingDetails MockingDetails=Mockito.MockingDetails(mockObject);
集合调用=mockingDetails.getInvocations();
List methodscaled=new ArrayList();
for(调用:调用){
methodsCalled.add(一个职业.getMethod().getName());
}
assertTrue(methodscaled.contains(“getGuid”);
问题:在我使用PowerMockito之前,它一直有效: 对于标准方法,它可以工作,但如果该方法是最终的,则该方法不存在于
mockingDetails.getInvocations()
(但即使没有出现在
mockingDetails.getInvocations()中
real
verify(mock).getGuid()
的工作方式很好

因此,如果您有任何想法/建议,我们将非常高兴


关于

我实际上使用反射完成了这项工作……我无法使用匹配器,但我已经获得了原始参数。我的测试验证了中间代理和数据转换过程是否调用了真正的实现:

private void assertMethodExecuted(String testName, Object ...params) throws Exception {
    assertManagerMethodExecuted(getMethodName(testName), params);
}

private void assertManagerMethodExecuted(String methodName, Object[] params) throws Exception {
    MiGRPCManagerImpl manager = Whitebox.getInternalState(server, MiGRPCManagerImpl.class);
    Method method = Arrays.stream(manager.getClass().getMethods())
        .filter(each -> each.getName().equals(methodName))
        .findFirst()
        .orElse(null);
    MiGRPCManagerImpl verify = Mockito.verify(manager, new VerificationMode() {

        @Override
        public void verify(VerificationData data) {
            assertEquals(1, data.getAllInvocations().size());
            data.getAllInvocations().get(0).getMethod().equals(method);
        }

        @Override
        public VerificationMode description(String description) {
            return this;
        }
    });

    if (params.length == 0) {
        method.invoke(verify);
    } else if (params.length == 1) {
        method.invoke(verify, params[0]);
    } else {
        method.invoke(verify, params);
    }

}

private String getMethodName(String testName) {
    return testName.substring(testName.indexOf("_") + 1, testName.length());
}
测试是这样的:

@Test
public void test_disconnectFromGui() throws SecurityException, Exception {
    String ip = factory.newInstance(String.class);
    String userName = factory.newInstance(String.class);
    fixture.disconnectFromGui(userName, ip );
    assertMethodExecuted(new Object() {}.getClass().getEnclosingMethod().getName(), userName, ip );
}

这对我使用常规Mockito是有效的(我使用它来验证是否调用了android.bluetooth.BluetoothGatt中的隐藏方法“refresh()”:

private void assertMethodInvoked(@NonNull Object,
@非空字符串methodName,
@非Null VerificationMode(验证模式)引发异常{
最终方法Method=object.getClass().getDeclaredMethod(methodName);
最终对象验证=验证(对象,验证模式);
方法调用(验证);
}

这完全是对语法的滥用,但是
meth.invoke(Mockito.verify(mockObject))
有效吗?对于这个想法,不幸的是,它什么都不起作用:我以前可以调用或不调用getGuid方法,但行似乎没有任何效果meth.invoke(Mockito.verify(mockObject))会更正确,就像Mockito.verify(meth.invoke)一样(mockObject));您传递的不是mock对象,而是执行的结果。虽然我无法使其工作,但以太…请确保您从PowerMock或Mockito获取
对象。只需使用SimpleObj.Class,它将看到该方法是最终的,而不会返回对存根的引用。