Android,PowerMock:文件夹 androidTest 中的测试 class:javassist.NotFoundException:
Android, PowerMock: Test class in folder androidTest: javassist.NotFoundException:
我想在我的 Android 应用程序上测试静态方法。
我的build.gradle:
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile "org.mockito:mockito-android:2.7.21"
androidTestCompile "org.powermock:powermock-api-mockito:1.6.6"
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
}
我的 Android 应用的 PowerMock 测试:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest(StringUtil.class)
public class StringUtilInstrumentedTest {
private Context context;
@Before
public void init() {
context = InstrumentationRegistry.getTargetContext();
}
@Test
public void decliningAge() {
mockStatic(StringUtil.class);
// use Mockito to set up your expectation
String expected = "first";
when(StringUtil.calcAge(context, 1)).thenReturn(expected);
assertThat(StringUtil.calcAge(context, 1), is(expected));
}
}
但是当我尝试开始测试时,抛出异常:
I/TestRunner(32154): java.lang.ClassNotFoundException: com.mycompany.StringUtilInstrumentedTest
I/TestRunner(32154): at java.lang.Class.classForName(Native Method)
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
I/TestRunner(32154): Caused by: java.lang.IllegalStateException: Failed to transform class with name com.mycompany.StringUtilInstrumentedTest. Reason: com.mycompany.StringUtilInstrumentedTest
org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java
I/TestRunner(32154): Caused by: javassist.NotFoundException: com.mycompany.StringUtilInstrumentedTest
I/TestRunner(32154): at javassist.ClassPool.get(ClassPool.java:452)
为什么会出现此错误?
如果 StringUtilInstrumentedTest
位于 src/test/java
中,则可以使用 PowerMock。但是,如果它在 src/androidTest/java
中,那么它将不起作用。
PowerMock 不适用于 Android 仪器测试。参考这个:https://github.com/powermock/powermock/issues/594
Sorry for the late response. PowerMock won't work on Android if you run it on a device since PowerMock is using JVM byte code manipulation. It will work if you run it on a JVM though.
我想在我的 Android 应用程序上测试静态方法。
我的build.gradle:
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile "org.mockito:mockito-android:2.7.21"
androidTestCompile "org.powermock:powermock-api-mockito:1.6.6"
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
}
我的 Android 应用的 PowerMock 测试:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest(StringUtil.class)
public class StringUtilInstrumentedTest {
private Context context;
@Before
public void init() {
context = InstrumentationRegistry.getTargetContext();
}
@Test
public void decliningAge() {
mockStatic(StringUtil.class);
// use Mockito to set up your expectation
String expected = "first";
when(StringUtil.calcAge(context, 1)).thenReturn(expected);
assertThat(StringUtil.calcAge(context, 1), is(expected));
}
}
但是当我尝试开始测试时,抛出异常:
I/TestRunner(32154): java.lang.ClassNotFoundException: com.mycompany.StringUtilInstrumentedTest
I/TestRunner(32154): at java.lang.Class.classForName(Native Method)
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
I/TestRunner(32154): Caused by: java.lang.IllegalStateException: Failed to transform class with name com.mycompany.StringUtilInstrumentedTest. Reason: com.mycompany.StringUtilInstrumentedTest
org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java
I/TestRunner(32154): Caused by: javassist.NotFoundException: com.mycompany.StringUtilInstrumentedTest
I/TestRunner(32154): at javassist.ClassPool.get(ClassPool.java:452)
为什么会出现此错误?
如果 StringUtilInstrumentedTest
位于 src/test/java
中,则可以使用 PowerMock。但是,如果它在 src/androidTest/java
中,那么它将不起作用。
PowerMock 不适用于 Android 仪器测试。参考这个:https://github.com/powermock/powermock/issues/594
Sorry for the late response. PowerMock won't work on Android if you run it on a device since PowerMock is using JVM byte code manipulation. It will work if you run it on a JVM though.