当我的第一个方法参数是String类型时,如何使用Powermock的Whitebox.invokeMethod(对象实例,对象...参数)?

问题描述 投票:0回答:1

我想不明确命名我在invokeMethod()参数中调用的方法。 Powermock提供了一个重载的invokeMethod(),它根据传递的参数推断出方法。

invokeMethod(Object instance, Object... arguments)

我遇到的问题是我的第一个参数是String类型。这会调用带有签名的invokeMethod()

invokeMethod(Object instance, String methodToExecute, Object... arguments)

这是测试模型......

@Test
public void thisIsATest() throws Exception{
    TheClassBeingTested myClassInstance = new TheClassBeingTested();
    String expected = "60";
    String firstArgument = "123A48"; 

    ReturnType returnedTypeValue = Whitebox.invokeMethod(myClassInstance, firstArgument, AnEnum.TypeA);
    String actual = returnedTypeValue.getTestedField();
    assertEquals("Expected should be actual when AnEnum is TypeA", expected, actual);
}

这给了我错误,

org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name '123A48' with parameter types: [ AnEnum ] in class TheClassBeingTested.`

我通过将第一个参数的类型更改为Object来实现它,但这让我觉得很脏。

@Test
public void thisIsATest() throws Exception{
    TheClassBeingTested myClassInstance = new TheClassBeingTested();
    String expected = "60";
    Object firstArgument = "123A48"; 

    ReturnType returnedTypeValue = Whitebox.invokeMethod(myClassInstance, firstArgument, AnEnum.TypeA);
    String actual = returnedTypeValue.getTestedField();
    assertEquals("Expected should be actual when AnEnum is TypeA", expected, actual);
}

是否有正确的方法将String类型作为第一个参数传递,而不是将我的方法名称硬编码到invokeMethod()调用中?我在Powermock文档或论坛中没有发现任何问题,但它肯定不是那么罕见。

java unit-testing junit powermock white-box
1个回答
0
投票

你真正需要做的是查看TheClassBeingTested.java。错误消息告诉您问题是Whitebox.invoke方法无法在通过反射创建的TheClassBeingTested中找到名为“123A48”的方法。在这种情况下,我认为你选择的invokeMethod正在寻找参数(Object classUnderTest,String methodName,Object ... parameters)。

尝试这样的事情:

public class TheClassBeingTested {
    private String foo;

    public void setFoo(String fooValue) {
        foo = fooValue;
    }

    public String getFoo() {
        return foo;
    }

}

然后你可以像这样用Whitebox测试:

public class TheClassBeingTestedTester {

    @Test
    public void thisIsATest() throws Exception {
        TheClassBeingTested toBeTested = new TheClassBeingTested();
        String theMethodToTest = "setFoo";
        String expectedFooValue = "foo bar baz";

        ReturnType returnedTypeValue = Whitebox.invokeMethod(toBeTested, theMethodToTest, expectedFooValue);
        String actual = returnedTypeValue.getTestedField();
        assertEquals("Expected " + expected + " but found " + actual, expected, actual);
     }
}

希望有所帮助。

...编辑的回复如下

由于我没有仔细阅读你的问题,同时也在进行其他开发,我忽略了这一点。

在这种情况下,我会对您的测试进行以下修改,以避免调用方法歧义问题:

@Test
public void thisIsATest() throws Exception{
    TheClassBeingTested myClassInstance = new TheClassBeingTested();
    String expected = "60";

    Object[] parameters = new Object[]{"123A48", AnEnum.TypeA};

    ReturnType returnedTypeValue = Whitebox.invokeMethod(myClassInstance, parameters);

    String actual = returnedTypeValue.getTestedField();
    assertEquals("Expected should be actual when AnEnum is TypeA", expected, actual);

}

这样,就消除了歧义,以便invokeMethod(Object instance,Object ... arguments)只能看到对象数组,这是方法签名告诉编译器所期望的。尽管String是一个Object,但在方法签名反射中,java.lang.reflect遵循第二个签名,它认为您试图告诉它使用您希望它使用的签名。

希望这个答案更符合您的要求。

© www.soinside.com 2019 - 2024. All rights reserved.