如何使用mockito模拟java反射方法及其方法

问题描述

我在 Spring Boot (2.4.x) 和 JUnit 4 中使用 Mockito (3.6.x) 进行单元测试。我需要测试下一个代码块:

MethodSignature methodSignature = powermockito.mock(MethodSignature.class);
powermockito.when((MethodSignature) proceedingJoinPointTest.getSignature()).thenReturn(methodSignature);
Method method = powermockito.mock(Method.class);

我已经尝试过使用其他解决方案(也使用 StackOverflow help),我现在正在使用 powermockito

当我这样做时:

CustomAnnotation customAnnotation = powermockito.mock(CustomAnnotation.class);
powermockito.when(method.getAnnotation(CustomAnnotation.class)).thenReturn(customAnnotation);

它有效。但是当我这样做时:

powermockito.when(method.getAnnotation(CustomAnnotation.class)).thenReturn(new CustomAnnotation() {
    // Implementation
});

失败了。我也试过:

{{1}}

但它也失败了。你有什么解决办法吗?

非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)