gpt4 book ai didi

android - Powermockito可以与Android Studio 1.2一起使用吗

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:30 25 4
gpt4 key购买 nike

我正在使用 Android Studio 1.2 和 com.android.tools.build:gradle:1.2.2 插件。

尝试 1

我在我的 app/build.gradle 中包含以下内容:

androidTestCompile ('com.google.dexmaker:dexmaker-mockito:1.2')
androidTestCompile ('org.powermock:powermock-mockito-release-full:1.6.2')

但是 PowerMockito 包无法导入:

error: cannot find symbol
PowerMockito.mockStatic(DatastoreFactory.class);
^

尝试 2

我在我的 app/build.gradle 中包含以下内容:

androidTestCompile ('org.powermock:powermock-api-mockito:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}

androidTestCompile ('org.powermock:powermock-module-junit4:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}

这是此 q/a 的试错分支:AndroidStudio/Gradle with powermock

这可以编译,但是当运行 Mockito 时会出现运行时错误:

java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils
at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49)
at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:24)
at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1285)
at org.mockito.Mockito.mock(Mockito.java:1163)
at com.mdsol.naga.FormPusherTest.setUp(FormPusherTest.java:40)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

有人在 Android Studio 1.2 上成功使用 Powermock 吗?请分享您的 build.gradle - 谢谢!

最佳答案

应该已经好了。至少在我的情况下它工作正常。

dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.0.5-beta'
testCompile 'com.android.support:support-v4:22.0.0'
testCompile ('org.powermock:powermock-api-mockito:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
testCompile ('org.powermock:powermock-module-junit4:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
}

此外,请记住在 Android Studio 的 Build Variants 的 Test Artifact 部分中选择 Unit Tests。您需要确保使用正确的项目结构、Gradle 版本和 testCompile。

你可以在这里找到关于这个的文章: http://vexdev.com/2015/05/06/unit-testing-android/

以及整个项目: https://github.com/vexdev/AndroidUnitTest

关于android - Powermockito可以与Android Studio 1.2一起使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30057310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com