1

I want to use the latest JUnit version:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.25.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.1.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>

But when I compile the code I get this exception:

ar 15, 2019 5:42:45 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;

Do you know how I can fix this? I use Java 11.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Code you are using that implements the Jupiter package? – gmanrocks Mar 15 '19 at 16:33
  • Possible duplicate of https://stackoverflow.com/questions/53926264/nosuchmethoderror-org-junit-platform-commons-util-reflectionutils-trytoloadclas – Prasann Mar 15 '19 at 16:40
  • you might need junit-platform-commons because that is a dependency for Junit-Jupiter-api and apiguardian-api and lastly opentest4j then for junit-jupiter-engine you need junit-platform-engine. And that is all the dependencies. – gmanrocks Mar 15 '19 at 16:46

1 Answers1

2

This error is caused by the Spring's dependency management starter poms and your pom.xml configuration. Spring-boot-starter-test is fetching JUnit4, which you need to exclude.

You should be able to fix it if you use this pom.xml config:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.25.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
              <exclusion>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
              </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
hovanessyan
  • 30,580
  • 6
  • 55
  • 83