ArrayStoreException:sun.reflect.annotation.TypeNotPresentExceptionProxy android PowerMock JUnit

Van*_*nsi 8 java junit android mockito powermock

我正在使用PowerMock和Mockito来测试静态函数,如下所示.它过去一直很好,直到今天它正在抛出下面提到的这个例外.

// this test case need to mock static methods so it uses PowerMock
@RunWith(PowerMockRunner.class)
// this static methods to be mocked are on Environment so that must be 'prepared'
@PrepareForTest({Environment.class, Build.class, Build.VERSION.class})
public class FileUtilityUnitTest {
    //This uses the JUnit TemporaryFolder Rule to create
    // (and discard on completion) a directory for the assertions.
    @Rule
    TemporaryFolder storageDirectory = new TemporaryFolder();
    File nonExistentDirectory;
    File existentDirectory;

    @Before
    public void setup(){
        PowerMockito.mockStatic(Environment.class);
        PowerMockito.mockStatic(Build.VERSION.class);
        nonExistentDirectory = Mockito.mock(File.class);
        //so the exists method tends to be false
        when(nonExistentDirectory.exists()).thenReturn(false);
        existentDirectory = storageDirectory.getRoot();
    }

    @Test
    public void test_is_external_storage_writable(){
        when(Environment.getExternalStorageState()).thenReturn(Environment.MEDIA_MOUNTED);
        assertTrue("External storage mounted ", Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED);
    }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪如下.

    Internal Error occured.
java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
    at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724)
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
    at java.lang.Class.createAnnotationData(Class.java:3521)
    at java.lang.Class.annotationData(Class.java:3510)
    at java.lang.Class.getAnnotation(Class.java:3415)
    at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:150)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:93)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Run Code Online (Sandbox Code Playgroud)

任何人都可以对此有所了解.我一直在谷歌搜索,但无法找到一个很好的参考.提前致谢.

Van*_*nsi 1

我在这里有一个观察要注意。我在某处读到这是由于构建生成的 android.jar 造成的。

不知道原因,但我尝试将compileVersion从26更改为25(26或25没有意义。我只是更改为随我安装的另一个版本。)并重建项目。

相同的测试用例再次开始工作,没有失败。