PowerMock与Mockito和JUnit一起模拟在函数中调用的Class方法,从而产生ArrayStoreException

时间:2019-10-23 16:13:21

标签: java junit mocking powermock

我正在尝试将PowerMock与Mockito和JUnit一起用于测试我的项目,该项目涉及内部调用静态类方法的方法。

在我的gradle.bulild文件中,我具有以下依赖项:

testCompile group: 'org.mockito', name: 'mockito-core', version: '2.8.9+'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.2'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.2'

在我的考试课上,我有以下内容:

import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassWithStaticMethod.class})

public class MyTest {
  @Test
  public void testMethod() {
    PowerMockito.mockStatic(ClassWithStaticMethod.class);

    //my test logic
  }
}

运行测试类时,出现以下错误:

  

java.lang.ArrayStoreException:sun.reflect.annotation.TypeNotPresentExceptionProxy       在sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724)       在sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531)       在sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355)       在sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286)处       在sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)处       在sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)处       在java.lang.Class.createAnnotationData(Class.java:3521)       在java.lang.Class.annotationData(Class.java:3510)       在java.lang.Class.getAnnotations(Class.java:3446)       在org.junit.runner.Description.createSuiteDescription(Description.java:124)       在org.junit.internal.runners.ErrorReportingRunner.getDescription(ErrorReportingRunner.java:28)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:91)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

与依赖项有关的问题是否不兼容,还是应该在代码中修复某些问题?

0 个答案:

没有答案