ExceptionInInitializerError with PowerMock and Robolectric



我想将Robolectric与PowerMock一起使用,但是在运行它们时遇到了一些麻烦。 我认为这是一个依赖性问题 - 现在我的jar只是在Eclipse中管理。

我以两种方式收到异常初始化器错误。 首先是这样的:

@RunWith(RobolectricTestRunner.class)
@PrepareForTest(Logger.class)
public class Logger_Test {
    @Mock Config config;
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

失败,并显示:

java.lang.ExceptionInInitializerError
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.internal.stubbing.defaultanswers.ReturnsMocks.<init>(ReturnsMocks.java:18)
at org.mockito.Answers.<clinit>(Answers.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.lang.Class.getEnumConstantsShared(Class.java:2942)
at java.lang.Class.enumConstantDirectory(Class.java:2963)
at java.lang.Enum.valueOf(Enum.java:191)
at sun.reflect.annotation.AnnotationParser.parseEnumValue(AnnotationParser.java:413)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:278)
at java.lang.reflect.Method.getDefaultValue(Method.java:720)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:99)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:66)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:202)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
at java.lang.reflect.Field.declaredAnnotations(Field.java:1014)
at java.lang.reflect.Field.getDeclaredAnnotations(Field.java:1007)
at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:175)
at org.junit.runners.model.FrameworkField.getAnnotations(FrameworkField.java:21)
at org.junit.runners.model.TestClass.addToAnnotationLists(TestClass.java:50)
at org.junit.runners.model.TestClass.<init>(TestClass.java:43)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:65)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:58)
at com.xtremelabs.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:213)
at com.xtremelabs.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:110)
at com.xtremelabs.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:97)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
... 56 more

它与PowerMockRunner一起工作,但抛出与规则相同的异常。

@PrepareForTest(Logger.class)
    public class Logger_Test {
    @Rule public PowerMockRule rule = new PowerMockRule();
    @Mock Config config;
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

失败,并显示:

java.lang.ExceptionInInitializerError
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.internal.stubbing.defaultanswers.ReturnsMocks.<init>(ReturnsMocks.java:18)
at org.mockito.Answers.<clinit>(Answers.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.lang.Class.getEnumConstantsShared(Class.java:2942)
at java.lang.Class.enumConstantDirectory(Class.java:2963)
at java.lang.Enum.valueOf(Enum.java:191)
at sun.reflect.annotation.AnnotationParser.parseEnumValue(AnnotationParser.java:413)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:278)
at java.lang.reflect.Method.getDefaultValue(Method.java:720)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:99)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:66)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:202)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
at java.lang.reflect.Field.declaredAnnotations(Field.java:1014)
at java.lang.reflect.Field.getAnnotation(Field.java:1000)
at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:222)
at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:82)
at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:69)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
... 59 more

添加@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })作为文档:https://github.com/robolectric/robolectric/wiki/Using-PowerMock

代替

@RunWith(RobolectricTestRunner.class)使用@RunWith(PowerMockRunner.class)

相关内容

  • 没有找到相关文章

最新更新