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

Ask*_*skQ 9 android mockito

尝试在AS上运行检测测试.

坚持这个错误:

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

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);
        }
       }
Run Code Online (Sandbox Code Playgroud)


的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')
    }
Run Code Online (Sandbox Code Playgroud)


更新: 评论后行 -

MockitoAnnotations.initMocks(本);

它构建正常(没有例外)但是被模拟的上下文现在为空.

Ask*_*skQ 10

工作:

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"
 }
Run Code Online (Sandbox Code Playgroud)

无需评论initiMocks

  • 仍然得到**java.lang.IllegalStateException:无法初始化插件:interface org.mockito.plugins.MockMaker** (12认同)
  • 这里重要的是“testCompile”需要“mockito-core”工件,而“androidTestCompile”需要“mockito-android”工件。我的问题是我对两者都使用了“mockito-android”,这导致了错误。 (4认同)

小智 5

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

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

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

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

希望这对某人有帮助。