PowerMockito.whenNew没有在android的Intent类上工作

时间:2015-07-23 17:13:20

标签: android android-intent powermock powermockito

我正在尝试使用模拟Intent对象但是当使用来自PowerMockito的whenNew时,我无法模拟构造函数。我已经尝试了所有可能的参数组合,但它只是没有用。

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,并在此answer中找到了解决方案。

更具体一点:请尝试在测试或课程级别添加@PrepareForTest注释,并为其提供构建您的意图的

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)
    }
}

希望这会有所帮助。