【发布时间】:2015-07-23 17:13:19
【问题描述】:
我正在尝试使用模拟 Intent 对象,但是当使用 PowerMockito 中的 whenNew 时,我无法模拟构造函数。我已经尝试了所有可能的参数组合,但它不起作用。
【问题讨论】:
标签: android android-intent powermock powermockito
我正在尝试使用模拟 Intent 对象,但是当使用 PowerMockito 中的 whenNew 时,我无法模拟构造函数。我已经尝试了所有可能的参数组合,但它不起作用。
【问题讨论】:
标签: android android-intent powermock powermockito
我也遇到过类似的问题,在这个answer找到了解决办法。
更具体地说:请尝试在测试或类级别添加@PrepareForTest注解,并为其提供构造您的Intent的类。
public class SomeClassThatCreatesIntent {
public void someMethodWithIntent() {
Intent i = new Intent();
}
}
然后测试类应该是这样的:
@RunWith(PowerMockRunner.class)
@PrepareForTest({SomeClassThatCreatesIntent.class})
public class SomeClassThatCreatesIntentTest {
@Test
public void test() {
// Some test that uses PowerMockito.whenNew(Intent.class)
}
}
希望这会有所帮助。
【讨论】: