Powermock private stubbing 出错了
Powermock private stubbing gows wrong
我正在尝试制作一个尽可能简单的示例,可以在 PowerMock 的官方页面 (here) 上找到。我正在对此进行部分模拟 class:
public class Simple {
public String doMe() {
return privateMethod();
}
private String privateMethod() {
return "POWERMOCK sucks";
}
}
并写了一个简单的测试 class :
@RunWith(PowerMockRunner.class)
@PrepareForTest(Simple.class)
public class ProcessorTest {
@Test
public void doMe() throws Exception {
Simple spy = PowerMockito.spy(new Simple());
PowerMockito.doReturn("hello").when(spy, "privateMethod");
String res = spy.doMe();
PowerMockito.verifyPrivate(spy, Mockito.times(1000)).invoke(
"privateMethod");
Assert.assertEquals( res, "hello");
}
}
但结果是这样的:
java.lang.AssertionError: expected [hello] but found [null]
Expected :hello
Actual :null
<Click to see difference>
at org.testng.Assert.fail(Assert.java:94)
所以 Powermock 不仅无法模拟 privateMethod
和 return 'null' 它被调用 1000 次也没关系。
如果我试图搞乱这样的嘲笑,它会变得更加令人毛骨悚然:
PowerMockito.doReturn(1).when(spy, "privateMethod");
所以我正在尝试 return 一个整数而不是 privateMethod
中的字符串。
然后我得到这个:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Integer cannot be returned by doMe()
doMe() should return String
***
所以由于某种原因,Powermock 正在尝试模拟 public doMe
方法。
有人知道什么是快乐吗?我不知道。
谢谢。
我的环境是:
Java 1.8,Mockito 1.10.19,Powermock 1.6.2
好的,我找到了解决方案,问题是 JUnit 的 @RunWith
实际上并没有做到这一点,所以我不得不从 PowerMockTestCase
扩展以使其工作。测试现在看起来像这样,而且效果很好:
@PrepareForTest(Simple.class)
public class ProcessorTest extends PowerMockTestCase {
@Test
public void doMe() throws Exception {
Simple spy = PowerMockito.spy(new Simple());
PowerMockito.doReturn("hello").when(spy, "privateMethod");
String res = spy.doMe();
PowerMockito.verifyPrivate(spy, Mockito.times(1)).invoke(
"privateMethod");
Assert.assertEquals( res, "hello");
}
}
我正在尝试制作一个尽可能简单的示例,可以在 PowerMock 的官方页面 (here) 上找到。我正在对此进行部分模拟 class:
public class Simple {
public String doMe() {
return privateMethod();
}
private String privateMethod() {
return "POWERMOCK sucks";
}
}
并写了一个简单的测试 class :
@RunWith(PowerMockRunner.class)
@PrepareForTest(Simple.class)
public class ProcessorTest {
@Test
public void doMe() throws Exception {
Simple spy = PowerMockito.spy(new Simple());
PowerMockito.doReturn("hello").when(spy, "privateMethod");
String res = spy.doMe();
PowerMockito.verifyPrivate(spy, Mockito.times(1000)).invoke(
"privateMethod");
Assert.assertEquals( res, "hello");
}
}
但结果是这样的:
java.lang.AssertionError: expected [hello] but found [null]
Expected :hello
Actual :null
<Click to see difference>
at org.testng.Assert.fail(Assert.java:94)
所以 Powermock 不仅无法模拟 privateMethod
和 return 'null' 它被调用 1000 次也没关系。
如果我试图搞乱这样的嘲笑,它会变得更加令人毛骨悚然:
PowerMockito.doReturn(1).when(spy, "privateMethod");
所以我正在尝试 return 一个整数而不是 privateMethod
中的字符串。
然后我得到这个:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Integer cannot be returned by doMe()
doMe() should return String
***
所以由于某种原因,Powermock 正在尝试模拟 public doMe
方法。
有人知道什么是快乐吗?我不知道。
谢谢。
我的环境是:
Java 1.8,Mockito 1.10.19,Powermock 1.6.2
好的,我找到了解决方案,问题是 JUnit 的 @RunWith
实际上并没有做到这一点,所以我不得不从 PowerMockTestCase
扩展以使其工作。测试现在看起来像这样,而且效果很好:
@PrepareForTest(Simple.class)
public class ProcessorTest extends PowerMockTestCase {
@Test
public void doMe() throws Exception {
Simple spy = PowerMockito.spy(new Simple());
PowerMockito.doReturn("hello").when(spy, "privateMethod");
String res = spy.doMe();
PowerMockito.verifyPrivate(spy, Mockito.times(1)).invoke(
"privateMethod");
Assert.assertEquals( res, "hello");
}
}