尝试在AS上运行检测测试。
坚持这个错误:
java.lang.IllegalStateException:无法初始化插件:interface 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);
}
}
的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(本);
它构建正常(无异常),但模拟的上下文现在为空。
答案 0 :(得分:7)
曾为:
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
答案 1 :(得分:0)
不要明确包含mockito,让powermock拉入它需要的东西。
答案 2 :(得分:0)
就我而言,我正在一个不使用Maven构建系统的项目中工作。所以这对我有用。
已导航到mockito的Maven存储库(已使用v2.26):https://mvnrepository.com/artifact/org.mockito/mockito-core/2.26.0。我下载了罐子。 在底部的同一页上,我查找了依赖项。对于嘲笑2.26.0,这些依赖项是:
在Eclipse中,我创建了一个包含四个jar文件的用户库,并将其添加到我的项目中。
NB :(创建库是可选的,您可以将jar直接添加到项目构建路径中)
希望这对某人有帮助。
答案 3 :(得分:0)
在为“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 - 查看编译的依赖项)