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

I am using PowerMock with Mockito to test a static function, as shown below. It worked well until today it throws 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 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 tell about this. I was googling but can't find a good link. Thanks in advance.

+7
source share
3 answers

I have a comment that should be noted here. Somewhere I read that this is related to android.jar, which is generated by the assembly.

I don’t know the reason, but I tried changing compileVersion from 26 to 25 (There is no value in 26 or 25. I just changed to another version that was installed with me.) And rebuild the project.

The same tests started working again.

+1
source

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

Now build.gradle looks like this:

 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 could not find a lot of comments about this, so I hope I can help someone.

+2
source

One possible reason that the Junit test throws a TypeNotPresentExceptionProxy exception is that some dependencies use compileOnly instead of an implementation.

For the reason that compiling the SDK version matters, perhaps because the SDK has removed the classes you are mocking.

0
source

Source: https://habr.com/ru/post/1272606/


All Articles