【问题标题】:java.lang.IllegalStateException: Could not initialize plugin: MockMakerjava.lang.IllegalStateException:无法初始化插件:MockMaker
【发布时间】:2017-06-10 14:03:01
【问题描述】:

尝试在 AS 上运行仪器测试。

遇到此错误:

java.lang.IllegalStateException:无法初始化插件:接口 org.mockito.plugins.MockMaker 在 org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66) 在 java.lang.reflect.Proxy.invoke(Proxy.java:393) 在 $Proxy4.isTypeMockable(未知来源)

ExampleInstrumentedTest.java

      @RunWith(AndroidJUnit4.class)
        public class ExampleInstrumentedTest {
        
            @Mock
            Context context;
     
  @Before
    public void init(){
        MockitoAnnotations.initMocks(this);
    }

        @Test
            public void testDisabledFlag()  {
                ChanceValidator chanceValidator  = new ChanceValidator(context);
                Validator.ValidationResult result = chanceValidator.validate(2);
                assertEquals(result, Validator.ValidationResult.NO_ERROR);
        }
       }

构建.gradle
apply plugin: 'com.android.application'

     android{
        ..
        defaultConfig {
                testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        
         testOptions {
                unitTests.returnDefaultValues = true
            }
    }
    
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        // Unit testing dependencies
        testCompile 'junit:junit:4.12'
        // Set this dependency if you want to use the Hamcrest matcher library
        testCompile 'org.hamcrest:hamcrest-library:1.3'
        // more stuff, e.g., Mockito
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile project(':mortar')
        compile project(':mockito-core-2.6.6')
    }
        

更新: 评论后-

MockitoAnnotations.initMocks(this);

构建良好(无异常),但模拟的上下文现在为空。

【问题讨论】:

  • 发布完整版ExampleInstrumentedTest
  • @Intellij 这是一个完整的测试

标签: android android-studio gradle mockito android-library


【解决方案1】:

在为“mockito-core”添加传递依赖项后,我解决了这个问题。 我在eclipse中遇到了这个问题。我正在使用“mockito-core 3.8.0”和“mockito-junit-jupiter 3.8.0”。 起初,我尝试通过在 Project/Java Build Path 中将 JRE 更改为 JDK 来解决此问题((许多人已将此作为解决方案发布),但这并没有解决问题。 然后,我显式地为​​“mockito-core 3.8.0”添加了以下 3 个传递依赖项,并且成功了!

1. net.bytebuddy » byte-buddy  v1.10.20
2. net.bytebuddy » byte-buddy-agent  v1.10.20
3. org.objenesis » objenesis  v3.1

(https://mvnrepository.com/artifact/org.mockito/mockito-core/3.8.0 - 查看编译的依赖项)

【讨论】:

    【解决方案2】:

    就我而言,我正在处理一个不使用 maven 构建系统的项目。所以这对我有用。

    导航到 mockito 的 maven 存储库(使用 v2.26):https://mvnrepository.com/artifact/org.mockito/mockito-core/2.26.0。我下载了jar。 在底部的同一页面上,我查找了依赖项。对于 mockito 2.26.0,这些依赖项是:

    在 Eclipse 中,我创建了一个包含四个 jar 文件的用户库并将其添加到我的项目中。

    注意:(创建库是可选的,您可以将 jar 直接添加到项目构建路径中)

    希望这对某人有所帮助。

    【讨论】:

    • 这不起作用。 Mockito 在其 MANIFEST.MF 文件中有一个 Buddy-/Objenesis-依赖项列表以及要使用的显式版本。
    • 我尝试使用这些库的最新版本 (1.10.8 \ 3.0.1) 并替换,但在测试启动 Studio 时强制加载 (1.10.5 \ 2.6)
    【解决方案3】:

    不要明确包含 mockito,让 powermock 拉取它需要的东西。

    【讨论】:

    • 我也有同样的问题。我为 mockito-core 添加了依赖项,但没有为 mockito-android 添加依赖项,因为我没有在 Android 设备中运行测试。我只在 Linux 环境下运行测试。 testCompile "org.mockito:mockito-core:2.8.47"
    【解决方案4】:

    工作:

    dependencies { 
    def mockito_version = '2.7.1' // For local unit tests on your development machine
     testCompile "org.mockito:mockito-core:$mockito_version" // For instrumentation tests on Android devices and emulators
     androidTestCompile "org.mockito:mockito-android:$mockito_version"
     }
    

    无需评论 initiMocks

    【讨论】:

    • 仍然得到 java.lang.IllegalStateException: 无法初始化插件:接口 org.mockito.plugins.MockMaker
    • 这里重要的是testCompile 需要mockito-core 工件,androidTestCompile 需要mockito-android 工件。我的问题是我对两者都使用了mockito-android,这导致了错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2014-04-23
    • 2017-11-12
    相关资源
    最近更新 更多