【问题标题】:How to combine PowerMock and Robolectric如何结合 PowerMock 和 Robolectric
【发布时间】:2017-02-13 15:29:01
【问题描述】:

我有一个使用上下文检查互联网状态的静态方法。所以要模拟它,我需要使用 PowerMock。我正在使用 robolectric 来加快我的测试,因为我唯一需要嘲笑的是被嘲笑的东西。但我似乎无法配置 PowerMock(1.6.6) 和 Robolectric(3.2.2) 一起工作。这就是我注释我的课程的方式:

@RunWith(RobolectricTestRunner.class)
@org.robolectric.annotation.Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareForTest({Utils.class})

设置方法中的这一行:

PowerMockito.mockStatic(Utils.class);

但我收到此异常:

org.powermock.api.mockito.ClassNotPreparedException: 
The class com.zeyad.usecases.data.utils.Utils not prepared for test.
To prepare this class, add class to the '@PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or  method level. 

at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:32)
at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:38)
at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:10)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:56)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:71)
at com.zeyad.usecases.data.repository.stores.DataStoreFactoryJUnitTest.setUp(DataStoreFactoryJUnitTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:339)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:259)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:41)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:199)

我的 build.gradle 文件依赖:

testCompile "junit:junit:$junit"
testCompile "org.hamcrest:hamcrest-library:$hamcrest"
testCompile "com.android.support:support-annotations:$supportLibraryVersion"
testCompile "org.mockito:mockito-core:1.10.19"
testCompile "org.robolectric:robolectric:$robolectric"
testCompile "org.robolectric:shadows-support-v4:$robolectric"
testCompile "org.powermock:powermock-module-junit4:$powerMock"
testCompile "org.powermock:powermock-module-junit4-rule:$powerMock"
testCompile "org.powermock:powermock-api-mockito:$powerMock"
testCompile "org.powermock:powermock-classloading-xstream:$powerMock"

【问题讨论】:

    标签: java unit-testing powermock robolectric powermockito


    【解决方案1】:

    在 Robolectric wiki 上,您可以找到如何使用 PowerMock 设置 Roboeletric 的完整说明 https://github.com/robolectric/robolectric/wiki/Using-PowerMock

    【讨论】:

      【解决方案2】:

      有时答案就是退后一步,看看您使用 PowerMock 的“需求”。

      那么,您有一个需要 PowerMock 可测试的 静态 方法?!

      然后,只需避免使该代码静态。然后,您不再需要使用 PowerMock。

      简单地将其替换为接口、impl 类的组合(如果您“确切”需要它的一个实现,也可以是枚举单例)。

      我向你保证:不需要 PowerMock 是更好的选择;与花费大量时间让它与另一个字节码操作框架一起工作相比。

      或者换句话说:学习如何编写可测试代码,然后去做;而不是创建难以测试的代码,这需要大的 PowerMock 锤子变得可测试。观看这些videos 以了解如何做到这一点。

      我不是 RoboElectric 的专家,但我假设:你的问题很可能是两个框架都需要自己特定的 @Runner 才能工作。因此:也许这here 有帮助;正如答案所暗示的那样,一种使用 PowerMock 而不使用 PowerMockRunner 的方法。

      【讨论】:

      • 嗯,是的,你是对的。但是如果知道如何让 robolectric 和 PowerMock 一起工作也很好?
      • 如果我知道的话;我会把它写下来......但是,我有一个想法并更新了我的答案。也许这有帮助。
      猜你喜欢
      • 2012-11-30
      • 2016-09-25
      • 1970-01-01
      • 2013-12-31
      • 2016-07-09
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多