2017-04-09 4 views
1

Android 앱에서 정적 메서드를 테스트하고 싶습니다.Android, PowerMock : 폴더 androidTest에서 테스트 클래스 : javassist.NotFoundException :

내 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' 
} 

내 PowerMock 시험 :

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) 

가 왜 :

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

을하지만 테스트를 시작, 던져 예외를하려고 할 때 이 오류가 발생합니까?

답변

0

StringUtilInstrumentedTestsrc/test/java에있는 경우 PowerMock을 사용할 수 있습니다. 그러나 그것이 src/androidTest/java에 있으면 작동하지 않습니다.

PowerMock은 Android 계측 테스트에서 작동하지 않습니다. 이것을 참조하십시오 : https://github.com/powermock/powermock/issues/594

죄송합니다. PowerMock이 JVM 바이트 코드 조작을 사용하고 있기 때문에 장치에서 PowerMock을 실행하면 Android에서 PowerMock이 작동하지 않습니다. 그것은 당신이 비록 JVM에서 그것을 실행하면 작동합니다.

+0

예, "src/androidTest/java"폴더에서 StringUtilInstrumentedTest를 시작합니다. – Alexei