我不想在 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 文档或论坛中没有找到任何解决此问题的内容,但它肯定不会那么罕见。
最佳答案
您真正需要做的是查看 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 是一个对象,但在方法签名反射中, java.lang.reflect 遵循第二个签名,它感觉您试图告诉它使用而不是您希望它使用的签名。
希望这个答案能更好地满足您的要求。
关于java - 当我的第一个方法参数是String类型时,如何使用Powermock的Whitebox.invokeMethod(Object instance, Object...arguments)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768430/