3

I added the jupiter, mockito and jupiter-engine dependencies to the pom.xml file as you can see in the following clipboard:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.myapp</groupId>
    <artifactId>testingit</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>4.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.0</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>
</project>

My tests are under the src/test/java/com/example/myapp/testingit package: enter image description here

If I run the mvn test it does not find any tests:
enter image description here

Although I have tests:
enter image description here

What goes wrong?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
stacktrace2234
  • 1,172
  • 1
  • 12
  • 22
  • 1
    To use JUnit Jupiter you have to at least the version 2.22.2 of maven-surefire-plugin or better most recent (3.0.0-M7) ... because any version before does not work...also you should define all used plugins to most recent versions (check here: https://maven.apache.org/plugins/) ... – khmarbaise Dec 19 '22 at 17:28
  • 1
    The class name shouldn't matter in JUnit Jupiter. What is `@Test`? Can you confirm that you have `import org.junit.jupiter.api.Test`? – Mureinik Dec 19 '22 at 17:29
  • @Mureinik I use: `import org.junit.jupiter.api.Test;` – stacktrace2234 Dec 19 '22 at 17:30
  • @khmarbaise same result if I use 2.22.2, the 3.0.0-M7 version throws `java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener` – stacktrace2234 Dec 19 '22 at 17:32
  • 1
    See for details (https://youtu.be/IVwbrhYCLpc) also https://github.com/khmarbaise/youtube-videos/blob/main/episode-2/pom.xml .... that's also based on mixing junit jupiter versions 5.4.0 and 5.9.1 not related to surefire plugin – khmarbaise Dec 19 '22 at 18:17

1 Answers1

3

You're missing the junit-platform-surefire-provider dependency. Additionally, you have mismatching versions of junit-jupiter-api and junit-jupiter-engine, which may not be critical, but is probably a bad idea that can lead to hard-to-find bugs later on. You can solve both these issues by using the aggregate junit-jupiter dependency:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.myapp</groupId>
    <artifactId>testingit</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency> <!-- Here! -->           
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>4.6.1</version>
            <scope>test</scope>
        </dependency>
   </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>
</project>
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Seems like this fixed my problem. Thanks. – stacktrace2234 Dec 19 '22 at 17:42
  • Do not use `junit-platform-surefire-provider` this is handled by the surefire-plugin on it's own (use most recent versions of the plugin...) also lacking the definition of the version maven-compiler-plugin ... – khmarbaise Dec 19 '22 at 18:13