PowerMockito.whenNew 在 Android 的 Intent 类上不起作用

3

我正在尝试使用模拟的Intent对象。但是当使用PowerMockito中的whenNew时,我无法模拟构造函数。我已经尝试过所有可能的参数组合,但它就是不起作用。

1个回答

7

我曾遇到类似的问题,解决方法在这个答案中找到。

更具体地说,请尝试在测试或类级别添加@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)
    }
}

希望这能帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接