0
试图让PowerMockito去这里用RoboElectric进行Android上的单元测试。我想在测试中调用一个公共方法并验证一些行为,但我想更改该类中私有方法的响应。此示例:PowerMockito与RoboElectric私有方法不嘲弄
public class TestPrivate {
public String doTest() {
return test();
}
private String test() {
return "FOO";
}
}
我想运行一个名为doTest的测试,但使用PowerMockito来更改test()返回的内容。下面我尝试不起作用,测试()返回“FOO”的时候,我期望我的测试返回“BAR”
@RunWith(RobolectricTestRunner.class)
@PrepareForTest(TestPrivate.class)
public class WebSocketClientTest extends TestCase {
@Test
public void testConnect() throws Exception {
TestPrivate spy = PowerMockito.spy(new TestPrivate());
doReturn("BAR").when(spy, method(TestPrivate.class, "test"));
String response = spy.doTest();
assertEquals("BAR", response);
}
}
几件事情:
使用RoboElectric测试运行似乎确定与PowerMockito 1.6
这是我gradle这个进口提前
androidTestCompile 'junit:junit:4.11'
androidTestCompile 'org.robolectric:robolectric:2.4'
androidTestCompile 'org.powermock:powermock-mockito-release-full:1.6.0'
谢谢,有关于这个的其他职位,但他们没有工作,是岁。
谢谢汤姆 - 是的,我不明白赛跑者。这个程序让我使用电动跑步机进行测试:https://github.com/robolectric/robolectric/issues/1058 – bsautner