【问题标题】:PowerMockito.whenNew not working on android's Intent classPowerMockito.whenNew 不适用于 android 的 Intent 类
【发布时间】:2015-07-23 17:13:19
【问题描述】:

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

【问题讨论】:

    标签: android android-intent powermock powermockito


    【解决方案1】:

    我也遇到过类似的问题,在这个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)
        }
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      相关资源
      最近更新 更多