ExceptionInInitializerError with Mockito Mockito中的ExceptionInInitializerError

huangapple go评论80阅读模式
英文:

ExceptionInInitializerError with Mockito

问题

以下是您提供的代码的翻译:

我看到了很多类似的问题但似乎没有一个能够解决这个问题无论我尝试什么我都不断收到 `ExceptionInInitializerError` 错误

```java
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.web.client.RestTemplate;    
import org.springframework.web.client.RestTemplate;


@RunWith(MockitoJUnitRunner.class)
public class ClassTest {

    @Mock
    private RestTemplate mockedRestTemplate;


    @Before
    public void setup() {
	    MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test() {	
	    fail("Not IMMM");
    }

}

我还尝试过 @RunWith(SpringRunner.class),但仍然出现相同的错误。

堆栈跟踪如下:

java.lang.ExceptionInInitializerError
	at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
	at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:30)
	at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)
	// 更多的堆栈信息...

希望这能帮助您解决问题。

<details>
<summary>英文:</summary>

I have seen a number of similar questions, but none of them seem to answer this. I keep getting `ExceptionInInitializerError` no matter what I try do:

```java
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.web.client.RestTemplate;    
import org.springframework.web.client.RestTemplate;


@RunWith(MockitoJUnitRunner.class)
public class ClassTest {

    @Mock
    private RestTemplate mockedRestTemplate;


    @Before
    public void setup() {
	    MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test() {	
	    fail(&quot;Not IMMM&quot;);
    }

}

I have also tried @RunWith(SpringRunner.class) but still the same error.

The stacktrace is as shown:

java.lang.ExceptionInInitializerError
	at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.&lt;init&gt;(ConditionalStackTraceFilter.java:17)
	at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)
	at org.mockito.exceptions.base.MockitoException.&lt;init&gt;(MockitoException.java:30)
	at org.mockito.exceptions.misusing.MockitoConfigurationException.&lt;init&gt;(MockitoConfigurationException.java:18)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
	at org.mockito.internal.configuration.plugins.PluginRegistry.&lt;init&gt;(PluginRegistry.java:12)
	at org.mockito.internal.configuration.plugins.Plugins.&lt;clinit&gt;(Plugins.java:11)
	at org.mockito.internal.util.MockUtil.&lt;clinit&gt;(MockUtil.java:24)
	at org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues.&lt;init&gt;(ReturnsEmptyValues.java:58)
	at org.mockito.internal.stubbing.defaultanswers.ReturnsMoreEmptyValues.&lt;init&gt;(ReturnsMoreEmptyValues.java:51)
	at org.mockito.internal.stubbing.defaultanswers.ReturnsSmartNulls.&lt;init&gt;(ReturnsSmartNulls.java:40)
	at org.mockito.Answers.&lt;clinit&gt;(Answers.java:43)
	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 java.lang.Class.getEnumConstantsShared(Class.java:3320)
	at java.lang.Class.enumConstantDirectory(Class.java:3341)
	at java.lang.Enum.valueOf(Enum.java:232)
	at sun.reflect.annotation.AnnotationParser.parseEnumValue(AnnotationParser.java:483)
	at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:347)
	at java.lang.reflect.Method.getDefaultValue(Method.java:606)
	at sun.reflect.annotation.AnnotationType.&lt;init&gt;(AnnotationType.java:132)
	at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
	at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
	at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
	at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
	at java.lang.reflect.Field.declaredAnnotations(Field.java:1150)
	at java.lang.reflect.Field.declaredAnnotations(Field.java:1148)
	at java.lang.reflect.Field.getDeclaredAnnotations(Field.java:1139)
	at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:207)
	at org.junit.runners.model.FrameworkField.getAnnotations(FrameworkField.java:31)
	at org.junit.runners.model.TestClass.addToAnnotationLists(TestClass.java:84)
	at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:71)
	at org.junit.runners.model.TestClass.&lt;init&gt;(TestClass.java:57)
	at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)
	at org.junit.runners.ParentRunner.&lt;init&gt;(ParentRunner.java:83)
	at org.junit.runners.BlockJUnit4ClassRunner.&lt;init&gt;(BlockJUnit4ClassRunner.java:65)
	at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.&lt;init&gt;(JUnit45AndHigherRunnerImpl.java:23)
	at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.&lt;init&gt;(JUnit45AndHigherRunnerImpl.java:23)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.mockito.internal.runners.util.RunnerProvider.newInstance(RunnerProvider.java:39)
	at org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:28)
	at org.mockito.runners.MockitoJUnitRunner.&lt;init&gt;(MockitoJUnitRunner.java:57)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.NullPointerException
	at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)
	at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.&lt;clinit&gt;(StackTraceFilter.java:21)
	... 65 more

答案1

得分: 2

我遇到了相同的问题。通过将po.xml中的依赖项更改如下来解决了它。首先,它是mokito-all,将其更改为mokito-core。

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>4.0.0</version>
  <scope>test</scope>
</dependency>
英文:

I Was experiencing the same issue. Fixed it by changing the dependency in the po.xml as follows. At first, it was mokito-all. changed it to mokito-core.

&lt;dependency&gt;
  &lt;groupId&gt;org.mockito&lt;/groupId&gt;
  &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
  &lt;version&gt;4.0.0&lt;/version&gt;
  &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年7月29日 10:28:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63145460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定