我想为Android应用程序编写静态方法的单元测试。我用这个Mocktio和PowerMock。
在Android文件夹“test”中:
myproject的/应用/ SRC /测试/ JAVA / COM / myproject的/机器人/
我有我的单元测试:
gh.addEdge(5,6)
@PrepareForTest(StringUtil.class) 公共类StringUtilTest {
@RunWith(PowerMockRunner.class)
}
我的build.gradle:
@Test
public void isCorrectEmailStub() {
mockStatic(StringUtil.class);
String INCORRECT_EMAIL_TO_CHECK = "incorrect_email_some.com";
when(StringUtil.isCorrectEmail(INCORRECT_EMAIL_TO_CHECK)).thenReturn(true);
assertThat(StringUtil.isCorrectEmail(INCORRECT_EMAIL_TO_CHECK), is(true));
}
项目建立成功。但是当我尝试运行测试“isCorrectEmailStub”时,我收到错误:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile "org.mockito:mockito-core:2.7.22"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile "org.mockito:mockito-android:2.7.21"
}
答案 0 :(得分:1)
仅适用于Mockito版本< 2.0。这项工作:
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile "org.mockito:mockito-core:1.10.19"