13

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?

Ivan dal Bosco
  • 3,233
  • 4
  • 19
  • 24
  • 1
    Generally either you have a corrupt test jar or are mixing jars from different versions of Spring. I suspect the latter and you are trying to outsmart the framework by providing unnecessary dependencies or version tags. – M. Deinum Jan 20 '21 at 10:15
  • This article may help .https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.4.0-M1-Release-Notes#junit-5s-vintage-engine-removed-from-spring-boot-starter-test – Kaz Jan 20 '21 at 11:58
  • Show your pom.xml. Without it all help is nothing but a guess. – johanneslink Jan 20 '21 at 13:35
  • Thanks a lot @M.Deinum, your interventions are as helpful as they already were ten years ago! – Ivan dal Bosco Jan 25 '21 at 08:21

7 Answers7

12

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

Oozeerally
  • 842
  • 12
  • 24
3

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
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Sandeep Jain
  • 1,019
  • 9
  • 13
2

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.

Ivan dal Bosco
  • 3,233
  • 4
  • 19
  • 24
1

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>
LoBo
  • 212
  • 1
  • 2
  • 9
1

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.

Vitaly Chura
  • 704
  • 8
  • 13
0

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> 
saba
  • 332
  • 2
  • 14
0

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>
saba
  • 332
  • 2
  • 14