您需要一个Method对象和一个Class对象。根据您的评论,Mockito无法模拟方法,因此您需要一个真正的方法。我还没有测试过,但是我相信这会起作用。代替:
when(mockContext.getMethod().getName()).thenReturn("someMethod");when(mockContext.getMethod().getDeclaringClass().getName()).thenReturn("someClass");你需要:
// any method will do, but here is an example of how to get one.Method testMethod = this.getClass().getMethod("logCallTest");when(mockContext.getMethod()).thenReturn(testMethod);显然,
getName()将不再返回“
someMethod”,并且
getDeclaringClass().getName()将返回此测试类的名称(在示例中),但是尽管您无法选择它们返回的内容,但是它们返回的内容仍是确定性的,因此您应该能够验证任何内容你需要。(当然,如果您需要监视或验证是否已对Method对象本身进行了调用,则仍然会遇到问题。)



