Maven: NoClassDefFoundError: org.springframework.test.context.junit4.SpringJUnit4ClassRunner

◇◆丶佛笑我妖孽 提交于 2019-12-01 02:33:30

In a comment you've said that project compiles, but tests don't run. Maven-surefire-plugin may be the culpit (as it was in my case). I was getting the same error, but after a little digging I knew that:

java.lang.NoClassDefFoundError: Could not initialize class
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.

was caused by:

java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>

which was thrown because maven-surefire-plugin wasn't picking test framework provider from the classpath but instead supplied its own outdated junit provider.

I get rid of the error by specifying JUnit artifact name:

<plugins>
...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>{your.surefire.version}</version>
        <configuration>
            <junitArtifactName> junit:junit:{your.junit.version} </junitArtifactName>
        </configuration>
    </plugin>
...
</plugins>

I dont know why but in my case spring-boot-starter-test comes with junit 4.10 and I find that is compiled with 4.12, so after add

 <dependency> 
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version> 
</dependency>

Works fine. Maybe a misconfiguration in pom of spring-boot

For anyone looking at this I had a similar problem and the issue had to do with a logging impl conflict that was pulled in from maven transitive dependencies in spring boot. Once I excluded spring-boot-starter-logging it fixed the issue.

SpringJUnit4ClassRunner most likely wasn't able to initialize in the classloader from the logging conflict and the NoClassDefFoundError was thrown at another point in the code because of that.

I found a solution, not an answer to my question.

The solution was to break the project up in stages.

  • First, I created three standalone projects and ran the relevant unit tests on each. (Obviously this has to be done in the correct order to ensure dependency)

  • Second, I created the parent pom and depency management.

  • Third, I brought each standalone project under the paren one by one. I gradually reduced the child pom dependencies through an iterative series of testing.

I still have no idea why the above structure failed. However, since everything now works, there is no reason to continue reduction until I finally break my webapp again.

I recently encountered this. I just updated the version from 3.0.7.RELEASE to 3.1.1.RELEASE.

I encountered the same error in Spring Boot. I just changed the spring-boot-starter-parent version from 1.5.6.RELEASE to 1.5.10.RELEASED.

This spring boot version already contains the class below:

org.springframework.test.context.junit4.SpringJUnit4ClassRunner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!