\n
Results :\nTests run: 0, Failures: 0, Errors: 0, Skipped: 0
\n\nsrc.main.java.Arithmetics.java
\npublic class Arithmetics\n{\n public static int add(int a, int b)\n {\n return a + b;\n }\n}\n
\nsrc.test.java.ArithmeticsTest.java
\nimport org.junit.jupiter.api.RepeatedTest;\nimport org.junit.jupiter.api.Test;\nimport static org.junit.jupiter.api.Assertions.*;\n\npublic class ArithmeticsTest\n{\n @Test\n public void testAdd()\n {\n assertEquals(4, Arithmetics.add(2, 3));\n }\n}\n
\npom.xml
\n<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0"\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\n <modelVersion>4.0.0</modelVersion>\n\n <groupId>groupId</groupId>\n <artifactId>Test</artifactId>\n <version>1.0-SNAPSHOT</version>\n\n <properties>\n <maven.compiler.source>15</maven.compiler.source>\n <maven.compiler.target>15</maven.compiler.target>\n </properties>\n\n <dependencies>\n\n <dependency>\n <groupId>org.junit.jupiter</groupId>\n <artifactId>junit-jupiter</artifactId>\n <version>RELEASE</version>\n <scope>test</scope>\n </dependency>\n\n <dependency>\n <groupId>org.junit.jupiter</groupId>\n <artifactId>junit-jupiter</artifactId>\n <version>RELEASE</version>\n <scope>compile</scope>\n </dependency>\n\n </dependencies>\n \n</project>\n
\n","author":{"@type":"Person","name":"Denys_newbie"},"upvoteCount":0,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"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.
\nThe surefire plugin version is the main culprit. Default with maven (2.12.4) will not work with junit-5 jupiter engine.
\nSo 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>\n <pluginManagement>\n <plugins>\n <plugin>\n <artifactId>maven-surefire-plugin</artifactId>\n <version>2.22.1</version>\n </plugin>\n </plugins>\n </pluginManagement>\n</build>\n
\n","author":{"@type":"Person","name":"code_mechanic"},"upvoteCount":1}}}Reputation: 1160
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
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
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:
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
This is wrong because you use the junit5 dependencies as well as the imports that correspond to the junit 5.
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