Denys_newbie
Denys_newbie

Reputation: 1160

My JUnit tests don't work when I execute them via maven

I use Maven on my test project and I wanted to test test option in Maven's lifecycle, but my JUnit test failed. I have a class named Arithmetics in src.main.java package and a class named ArithmeticsTest in src.test.java package.

When I run ArithmeticsTest on my own using IntelliJ IDEA everything works OK, and I have expected java.lang.AssertionError, so why I don't have such when I run test option in maven?

Console output:


T E S T S

Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

src.main.java.Arithmetics.java

public class Arithmetics
{
    public static int add(int a, int b)
    {
        return a + b;
    }
}

src.test.java.ArithmeticsTest.java

import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ArithmeticsTest
{
    @Test
    public void testAdd()
    {
        assertEquals(4, Arithmetics.add(2, 3));
    }
}

pom.xml

<?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>groupId</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>
    
</project>

Upvotes: 0

Views: 80

Answers (2)

code_mechanic
code_mechanic

Reputation: 1148

As other answer already pointed out few things which may go wrong in your case, I am just adding the solution to your pom xml.

The surefire plugin version is the main culprit. Default with maven (2.12.4) will not work with junit-5 jupiter engine.

So just add the plugin in your with version 2.22.1 in your pom, it should work after that, assuming your folder structure as per required (see other answer).

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42541

There are three wrong things I can spot based on your question, while they might not provide a complete answer I hope they'll will be a step in a right direction:

  1. src/main/java and src/test/java is not a package as you write. Its a folder layout supported by maven, basically , if you have a code in com.myorg.somepackage.Arithmetics Make sure you have the following layout at the level of actual physical folders:
src
 main
  java
    com
     myorg
       somepackage
         Arithmetics.java
 test
  java
    com
     myorg
       somepackage
         ArithmeticsTest.java

For both java files the package must be com.myorg.somepackage:

package com.myorg.somepackage;

public class Arithmetics {}

And

package com.myorg.somepackage;

public class ArithmeticsTest {}

I know it might be a wrong naming that you've used in the question, but still I have to state it because it might cause an actual issue

  1. The second issue is that for some reason you seem to configure your surefire plugin to use test ng which is an alternative to junit. It can happen because testng is placed as a dependency - I can only speculate because you don't really show the full surefire plugin configure and do not provide a full list of dependencies, but you've got and idea I believe :)

This is wrong because you use the junit5 dependencies as well as the imports that correspond to the junit 5.

  1. The dependencies on the junit 5 are completely messy:

You seem to have two dependencies on just the same with the different scope, its really a wrong thing to do. Make sure you use only the test scope and have all the relevant dependencies. Read here for instruction of how to configure the surefire plugin

In addition, for the sake of completeness of the answer, check that you use the recent version of surefire plugin, if its too old the chances are that it won't be able to run jupiter engine (junit 5)

Upvotes: 4

Related Questions