Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using PowerMock with Mockito for testing a static function as follow. It used to work good until today it is throwing this exception mentioned below.

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

And the stacktrace is as follows.

    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)

Can anyone share some light on this. I have been googling but cant find a good reference. Thanks in advance.

like image 921
Vansi Avatar asked Oct 13 '17 21:10

Vansi


1 Answers

I had the same problem today. I'm not sure what happened, but by updating the compileSdkVersion, buildToolsVersion and targetSdkVersion to match the support-annotations version, the issue is gone.

Now the build.gradle looks like:

android {
  compileSdkVersion 27
  buildToolsVersion "27.0.2"

  defaultConfig {
    ...
    targetSdkVersion 27
  }
}

dependencies {
  compile 'com.android.support:support-annotations:27.0.2'
  androidTestCompile 'com.android.support:support-annotations:27.0.2'
}

I couldn't find much comments about it, so I hope I can help someone.

like image 177
Maychell Fernandes Avatar answered Oct 10 '22 06:10

Maychell Fernandes