使用 PowerMockito returns null 而不是返回 List<String> 模拟私有方法调用:(不想执行私有方法)
Mocking private method call using PowerMockito returns null instead of returning List<String>: (Want not to execute private method)
我正在尝试使用 JUnit4 对方法进行单元测试。被测方法正在调用另一个私有方法,我想使用 PowerMockito 模拟它。
我的方法如下:
Class MyClass {
public List<String> myMethod(String name) throws IOException
{
... Few lines of code for setting variables
List<String> result = myPrivateMethod(a, b);
... Few more lines of code..
result.addAll(myPrivateMethod(c, d));
return result;
}
private List<String> myPrivateMethod(String a, String b) {
.....
}
}
我测试上面代码的单元测试方法如下:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class TestClass {
@Test
public void testMyMethod() throws Exception {
MyClass myClass = PowerMockito.spy(new MyClass());
PowerMockito.doReturn(new ArrayList<String>(){{add("temp");}}).when(myClass, "myPrivateMethod", "a", "b");
List<String> list = myClass.myMethod("someName");
assertEquals(list.size(), 1);
}
}
我期待行 PowerMockito.doReturn(new ArrayList(){{add("temp");}}).when(myClass, "myPrivateMethod", "a", "b"); 到 return 大小为 1 的列表。我确认执行不会进入私有方法,但我没有得到添加了一个值的列表。
上面的单元测试代码有什么问题,为什么我得到的是 null 而不是 PowerMockito.doReturn() 方法中提到的填充列表?
在您的测试中,您正在调用 myMethod
,而后者又会调用 myPrivateMethod
两次,请参阅:
List<String> result = myPrivateMethod(a, b);
...
result.addAll(myPrivateMethod(c, d));
但是您的测试只模拟了一次对 myPrivateMethod
的调用,因此流程如下所示:
myMethod
-> myPrivateMethod
其中参数是 a, b
- 这个 是 嘲笑和 myPrivateMethod
returns "temp"
myMethod
-> myPrivateMethod
其中参数是 c, d
- 这个 不是 模拟因此 myPrivateMethod
被执行
为了通过此断言:assertEquals(list.size(), 1);
您需要重新测试以模拟对 myPrivateMethod
的第二次调用。此外,对 "returns null" 的引用表明此处的 when
块:.when(myClass, "myPrivateMethod", "a", "b")
与 myMethod
中提供的实际参数不匹配。
这是一个工作示例:
public class MyClass {
public List<String> myMethod(String name) throws IOException {
List<String> result = myPrivateMethod("a", "b");
result.addAll(myPrivateMethod("c", "d"));
return result;
}
private List<String> myPrivateMethod(String a, String b) {
List<String> r = new ArrayList<>();
r.add(a);
r.add(b);
return r;
}
}
@Test
public void testMyMethod() throws Exception {
MyClass myClass = PowerMockito.spy(new MyClass());
PowerMockito.doReturn(new ArrayList<String>(){{add("temp");}})
.when(myClass, "myPrivateMethod", "a", "b");
PowerMockito.doReturn(new ArrayList<String>())
.when(myClass, "myPrivateMethod", "c", "d");
List<String> list = myClass.myMethod("someName");
assertEquals(1, list.size());
assertEquals("temp", list.get(0));
}
以上示例测试通过以下内容:
- junit: 4.12
- powermock-module-junit4: 2.0.2
- powermock-api-mockito2: 2.0.2
我正在尝试使用 JUnit4 对方法进行单元测试。被测方法正在调用另一个私有方法,我想使用 PowerMockito 模拟它。
我的方法如下:
Class MyClass {
public List<String> myMethod(String name) throws IOException
{
... Few lines of code for setting variables
List<String> result = myPrivateMethod(a, b);
... Few more lines of code..
result.addAll(myPrivateMethod(c, d));
return result;
}
private List<String> myPrivateMethod(String a, String b) {
.....
}
}
我测试上面代码的单元测试方法如下:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class TestClass {
@Test
public void testMyMethod() throws Exception {
MyClass myClass = PowerMockito.spy(new MyClass());
PowerMockito.doReturn(new ArrayList<String>(){{add("temp");}}).when(myClass, "myPrivateMethod", "a", "b");
List<String> list = myClass.myMethod("someName");
assertEquals(list.size(), 1);
}
}
我期待行 PowerMockito.doReturn(new ArrayList(){{add("temp");}}).when(myClass, "myPrivateMethod", "a", "b"); 到 return 大小为 1 的列表。我确认执行不会进入私有方法,但我没有得到添加了一个值的列表。
上面的单元测试代码有什么问题,为什么我得到的是 null 而不是 PowerMockito.doReturn() 方法中提到的填充列表?
在您的测试中,您正在调用 myMethod
,而后者又会调用 myPrivateMethod
两次,请参阅:
List<String> result = myPrivateMethod(a, b);
...
result.addAll(myPrivateMethod(c, d));
但是您的测试只模拟了一次对 myPrivateMethod
的调用,因此流程如下所示:
myMethod
->myPrivateMethod
其中参数是a, b
- 这个 是 嘲笑和myPrivateMethod
returns "temp"myMethod
->myPrivateMethod
其中参数是c, d
- 这个 不是 模拟因此myPrivateMethod
被执行
为了通过此断言:assertEquals(list.size(), 1);
您需要重新测试以模拟对 myPrivateMethod
的第二次调用。此外,对 "returns null" 的引用表明此处的 when
块:.when(myClass, "myPrivateMethod", "a", "b")
与 myMethod
中提供的实际参数不匹配。
这是一个工作示例:
public class MyClass {
public List<String> myMethod(String name) throws IOException {
List<String> result = myPrivateMethod("a", "b");
result.addAll(myPrivateMethod("c", "d"));
return result;
}
private List<String> myPrivateMethod(String a, String b) {
List<String> r = new ArrayList<>();
r.add(a);
r.add(b);
return r;
}
}
@Test
public void testMyMethod() throws Exception {
MyClass myClass = PowerMockito.spy(new MyClass());
PowerMockito.doReturn(new ArrayList<String>(){{add("temp");}})
.when(myClass, "myPrivateMethod", "a", "b");
PowerMockito.doReturn(new ArrayList<String>())
.when(myClass, "myPrivateMethod", "c", "d");
List<String> list = myClass.myMethod("someName");
assertEquals(1, list.size());
assertEquals("temp", list.get(0));
}
以上示例测试通过以下内容:
- junit: 4.12
- powermock-module-junit4: 2.0.2
- powermock-api-mockito2: 2.0.2