ClassNotFoundException on org.springframework.test.context.TestContextAnnotationUtils when upgrading Spring Boot to 2.4.2

Viewed 17441

In my POM I upgraded the version of Spring Boot from 2.3.5.RELEASE to 2.4.2. As a result, mvn clean test now fails with error

java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils
Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.TestContextAnnotationUtils

on a test that consists simply in

package ch.ge.ael.enu.mediation;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MediationApplicationTests {

    @Test
    void contextLoads() {
    }

}

Does anyone have a clue?

7 Answers

What resolved this issue for me was upgrading my spring-test dependency version from 5.2.9.RELEASE to 5.3.8

The problem described is related to conflicts between different Maven dependencies. We have had the same problem while trying to use the latest version of Spring Boot.

After analyzing the dependency tree and listing of dependencies you may get which is going out of version. Try to exclude from there and include as stand alone which help us getting in sync.

Maven commands:

mvn dependency:tree
mvn dependency:list

Marten Deinum's comment was correct : a dependency created havoc among the Spring JARs' versions. With Camel 3.6.0, Spring Boot 2.3.5.RELEASE was OK but Spring Boot 2.4.2 was not. Upgrading Camel to 3.7.1 did the trick.

Seems that you are missing dependencies. The class org.springframework.test.context.TestContextAnnotationUtils referenced from spring-boot-starter-test-2.4.2 is located in

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.3</version>
    </dependency>

I had to update spring-test version from 5.2.19.RELEASE to 5.3.17.

Not sure why TestContextAnnotationUtils class was not included in 5.2.19.RELEASE.

in my case the library version in my pom.xml had conflict with spring boot version so i removed the version from my dependency and let the spring use the configured version by default.

i changed :

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test-autoconfigure</artifactId>
    <version>2.6.2</version>
    <scope>test</scope>
</dependency>

to :

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test-autoconfigure</artifactId>
        <scope>test</scope>
    </dependency> 

in my case i added the below dependency with version, it caused all tests to fail. so i removed version tag.

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <version>5.6.3</version>
            <scope>test</scope>
        </dependency>

changed to :

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
Related