我正在尝试模拟一些基于反射的方法。您可以在下面看到详细信息,
被测类
public class TracerLog {
@AroundInvoke
public Object logCall(InvocationContext context) throws Exception {
Logger logger = new Logger();
String message = "INFO: Invoking method - "
+ context.getMethod().getName() + "() of Class - "
+ context.getMethod().getDeclaringClass();
logger.write(message);
return context.proceed();
}
}
测试
public class TracerLogTest {
@Mock
InvocationContext mockContext;
@Mock
Logger mockLogger;
@InjectMocks
private TracerLog cut = new TracerLog();
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void logCallTest() throws Exception {
when(mockContext.proceed()).thenReturn(true);
when(mockContext.getMethod().getDeclaringClass().getName()).thenReturn("someClass");
cut.logCall(mockContext);
verify(mockContext).proceed();
}
或
@Test
public void logCallTest() throws Exception {
when(mockContext.proceed()).thenReturn(true);
when(mockContext.getMethod().getName()).thenReturn("someMethod");
when(mockContext.getMethod().getDeclaringClass().getName()).thenReturn("someClass");
cut.logCall(mockContext);
verify(mockLogger).write(anyString());
verify(mockContext).proceed();
}
但是,测试失败并出现 NullPointerException。我知道我在反对模拟概念方面做错了,但我不明白它是什么。您能否对此进行一些说明,并建议我如何测试此方法?
谢谢。
最佳答案
您需要一个 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 对象本身进行了调用,您仍然会遇到困难。)
关于java - 模拟基于反射的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011129/