简体   繁体   English

Junit Mockito 初始化错误

[英]Junit Mockito error on initialization

I have looked everywhere online.我在网上到处找。 I have tried almost everything, from deleting STS to deleting all the code from my station and re-import everything back.我几乎尝试了所有方法,从删除 STS 到从我的站中删除所有代码并重新导入所有内容。 I played with dependencies, build path, properties, etc. but still nothing works.我玩过依赖项、构建路径、属性等,但仍然没有任何效果。

Running the tests on Maven by CLI is working fine, and other team members don't have that issue, so it is probably some configuration on my station.通过 CLI 在 Maven 上运行测试工作正常,其他团队成员没有这个问题,所以这可能是我站上的一些配置。

The problem:问题:

When I try to debug/run a unit test inside STS using JUnit v. 4 I get an error with the following stack trace:当我尝试使用 JUnit v. 4 在 STS 中调试/运行单元测试时,我收到以下堆栈跟踪错误:

java.lang.IllegalAccessError: class org.mockito.internal.creation.jmock.ClassImposterizer$1 cannot access its superclass org.mockito.internal.creation.cglib.MockitoNamingPolicy
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.mockito.internal.creation.jmock.ClassImposterizer.<clinit>(ClassImposterizer.java:38)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:43)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1285)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43)
at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:254)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

If anyone have any idea on what the problem might be...如果有人对问题可能是什么有任何想法......

Here is part of the POM:这是POM的一部分:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
    <relativePath>org.springframework.boot:spring-boot-starter-parent</relativePath>
</parent>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>com.mockrunner</groupId>
        <artifactId>mockrunner</artifactId>
        <version>0.3.1</version>
    </dependency>

spring-boot-starter-test 1.4.2.RELEASE uses Mockito 1.10.19 by default, org.mockito.internal.creation.jmock.ClassImposterizer doesn't exist in 1.10.19 . spring-boot-starter-test 1.4.2.RELEASE默认使用Mockito 1.10.19org.mockito.internal.creation.jmock.ClassImposterizer1.10.19中不存在。 It's now in the org.mockito.internal.creation.cglib package.它现在位于org.mockito.internal.creation.cglib包中。

Exclude org.mockito from spring-boot-starter-test , as follows:spring-boot-starter-test排除org.mockito ,如下:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.mockito</groupId>
                    <artifactId>mockito-core</artifactId>
                </exclusion>
            </exclusions>
</dependency>

And include mockito-all 1.9.5 as a dependency:并包含mockito-all 1.9.5作为依赖项:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>

This happens when there are multiple versions of mockito.当有多个版本的 mockito 时会发生这种情况。 Exclude mockito version which is being transitively brought by other dependencies.排除由其他依赖项传递的 mockito 版本。

<exclusion>
 <artifactId>mockito-all</artifactId>
 <groupId>org.mockito</groupId>
 </exclusion>

This worked in my case.这在我的情况下有效。

In my case I had added correct jars in project build path.就我而言,我在项目构建路径中添加了正确的 jars。 But the test case's run configuration classpath had different jars.但是测试用例的运行配置类路径有不同的 jars。 Correcting the run configuration of junit test worked for me.更正 junit 测试的运行配置对我有用。

in my case, while generating the Sonar report, it requires us to delete some jar files related to Mockito framework.就我而言,在生成声纳报告时,它要求我们删除一些与 Mockito 框架相关的 jar 文件。 So, while running the jUnits, I faced this issue and found out that two jar files (mentioned below) were not present in the codebase.因此,在运行 jUnit 时,我遇到了这个问题,发现代码库中不存在两个 jar 文件(如下所述)。

1. mockito-all-1.9.5.jar
2. mockito-core-1.10.19.jar

You need to navigate to {platform home}/ext/core/lib folder and check for the files.您需要导航到{platform home}/ext/core/lib文件夹并检查文件。 If not present, run ant customize all one of the above two jar files will be generated in the folder mentioned above.如果不存在,运行ant customize all一个,将在上述文件夹中生成。 Then run ant unittests and the second jar file will also be generated according to your Mockito version.然后运行ant unittests ,第二个 jar 文件也将根据您的 Mockito 版本生成。 Save these two files at some other location.将这两个文件保存在其他位置。 Then place these files again in {platform home}/ext/core/lib path and try.然后将这些文件再次放入{platform home}/ext/core/lib路径并尝试。 It will definitely work.它肯定会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM