模拟基于反射的调用 [英] Mocking Reflection based calls

查看:93
本文介绍了模拟基于反射的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟一些基于反射的方法。您可以在下面看到详细信息,

I am trying to mock some reflection based methods. Below you can see the details,

待测班级

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而失败。我明白我在反对嘲笑概念时做错了什么,但我不明白它是什么。你能否对它进行一些说明,并建议我如何测试这种方法?

But, the tests fail with a NullPointerException. I understand that I am doing something wrong against mocking concepts, but I do not understand what it is. Could you please throw some light on it and also suggest me how this method can be tested?

谢谢。

推荐答案

您需要一个Method对象和一个Class对象。根据你的评论,Mockito不能模拟一个方法,所以你需要一个真实的方法。我没有测试过这个,但我相信这会有用。而不是:

You need a Method object and a Class object. As per your comment, Mockito cannot mock a Method, so you'll need a real one. I haven't tested this, but I believe this would work. Instead of:

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对象本身进行了调用,那么您仍然会被卡住。)

Obviously, getName() will not return "someMethod" anymore and getDeclaringClass().getName() will return the name of this test class (in the example), but although you couldn't pick what they return, what they return is still deterministic, so you should be able to verify anything you need. (Of course, if you needed to spy or verify that a call was made on the Method object itself, you're still stuck.)

这篇关于模拟基于反射的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆