java.lang.IllegalStateException:无法初始化插件:MockMaker

java.lang.IllegalStateException: Could not initialize plugin: MockMaker

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

遇到这个错误:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker at org.mockito.internal.configuration.plugins.PluginLoader.invoke(PluginLoader.java:66) at java.lang.reflect.Proxy.invoke(Proxy.java:393) at $Proxy4.isTypeMockable(Unknown Source)

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);
        }
       }

build.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(这个);

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

工作:

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

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

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

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

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

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

希望这对某人有所帮助。

我在为 'mockito-core' 添加传递依赖后解决了这个问题。 我在日食中遇到了这个问题。我正在使用 '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 - 查看编译的依赖项)