How to test methods in a java project using a maven junit5 framework project in eclipse - maven

I created a new maven junit5 framework project to test existing java projects. I added the java project in build path of the newly created maven junit5 framework project. I right clicked the method I wanted to add junit test case for and selected new junit test case and changed the source folder to the new maven junit5 framework project src directory and left the rest of the options as default. Created the junit test and ran the test as a unit test without any issues(screenshot below). Running the same test using maven getting the error below. I added the surefire plugin in pom(below) but still getting the error below. Using eclipse.
-------------------------------------------------------------------------------
Test set: com.build.VersionInfoTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 s <<< FAILURE! - in com.build.VersionInfoTest
com.build.VersionInfoTest Time elapsed: 0.002 s <<< ERROR!
java.lang.NoClassDefFoundError: Lcom/build/VersionInfo;
Caused by: java.lang.ClassNotFoundException: com.build.VersionInfo
<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>UnitTesting</groupId>
<artifactId>com.unit.testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.unit.testing</name>
<description>Junit Tests</description>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<junit.platform.version>1.5.2</junit.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
Update : I cleaned up the pom(below) but now no tests discovered? when I run the project with junit the test is discovered?
[INFO] Scanning for projects...
[INFO]
[INFO] --------------< UnitTesting:com.unit.testing >---------------
[INFO] Building com.unit.testing 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------- -----------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # com.unit.testing ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # com.unit.testing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default- testResources) # com.unit.testing ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # com.unit.testing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) # com.unit.testing ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.build.VersionInfoTest
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 s - in com.build.VersionInfoTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.955 s
[INFO] Finished at: 2020-03-09T10:00:22-04:00
[INFO] ------------------------------------------------------------------------
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.6.0</junit.jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
package com.dbb.build
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import com.dbb.build.VersionInfo;
class VersionInfoTest {
VersionInfo versionInfo = VersionInfo.getInstance();
#Test
void getVersion() {
String version = versionInfo.getVersion();
System.out.println(version);
assertNotNull(versionInfo.getVersion(), "expected a return value of"+version+"but was null");
}
}
UPDATE:
[INFO] --- maven-resources-plugin:2.6:testResources (default- testResources) # DBB-Unit-Testing ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) # DBB-Unit-Testing ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Unit-Testing/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Unit-Testing/src/test/java/com/VersionInfoTest.java:[7,25] cannot find symbol
symbol: class VersionInfo
location: package com.build
[ERROR] /Unit-Testing/src/test/java/com/build/VersionInfoTest.java: [11,9] cannot find symbol
symbol: class VersionInfo
location: class com.build.TestVersionInfo
[ERROR] /Unit-Testing/src/test/java/com/ /build/VersionInfoTest.java: [11,35] cannot find symbol
symbol: variable VersionInfo
location: class com.build.TestVersionInfo
[INFO] 3 errors
Solution: Using junit-platform-console-standalone-1.5.2.jar and run units via command line. Looks like if we have a non maven project junit-platform-console-standalone seems to be a better option.

Here is a sample of some of my pom I use with Maven 3.x and tests executed as expected with JUnit 5 in Eclipse but also from command line:
Don't add too many Juniper artifacts, some will create some side effect if present.
Note also the updated version of the surefire plugin with had some issue in the past with JUnit5
<properties>
<!-- ensure proper encoding of source and resource files in the project -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit-5.version>5.6.0</junit-5.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
UPDATE:
You can find a small example here: https://gist.github.com/asa-git/8e34bbc51b5fcb09b7fab3efdaaa73c9
Note that I am using maven version 3.6.3 and a JDK 8.
Furthermore, when running from the command line on windows (but likewise on other systems), you need to make sure your JDK is on your path before any other JSE installed on your system.

Related

"mvn test" runs testng instead of Junit

When I run "mvn test" no tests are run and in the report I see that testng was somehow invoked - how is this possible ?
Ultimately mvn does not run any test.
Could someone help ? I dont have any testng library in the code. Maybe pom is incorrect or something is missing ?
Ultimately mvn does not run any test.
Could someone help ? I dont have any testng library in the code. Maybe pom is incorrect or something is missing ?
After running "mvn test" it invokes test ng:
Running cucumber.Options.TestRunner
Configuring TestNG with: org.apache.maven.surefire.testng.conf"
My pom looks like this:
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<properties>
</properties>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.9.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
</dependencies>
</project>
When I run mvn test command I get
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building APIframwork 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # APIframwork ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Test\eclipse-workspace\APIframwork\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # APIframwork ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # APIframwork ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Test\eclipse-workspace\APIframwork\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # APIframwork ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # APIframwork ---
[INFO] Surefire report directory: C:\Users\Test\eclipse-workspace\APIframwork\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cucumber.Options.TestRunner
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator#7006c658
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.506 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.381 s
[INFO] Finished at: 2021-10-13T15:12:56+02:00
[INFO] Final Memory: 15M/180M
[INFO] ------------------------------------------------------------------------```

-Dcucumber.options ignored with mvn test

I'm using "mvn test" to run cucumber tests, but when I try to pass options on the command line with
-Dcucumber.options=..., the options are ignored and the ones specified in #CucumberOptions in the runner class are used instead. For example, if I just try to display cucumber help, it ignores it and just runs the tests:
C:\Maven\ArchCuke\untitled>mvn test -Dcucumber.options="--help"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< org.example:untitled >------------------------
[INFO] Building untitled 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # untitled ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Maven\ArchCuke\untitled\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) # untitled ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # untitled ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) # untitled ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Maven\ArchCuke\untitled\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) # untitled ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.example.steps.CucumberTestRunner
Scenario: Add two numbers # features/arith.feature:4
Given A Calculator # org.example.steps.ArithSteps.aCalculator()
When I add 2 and 2 # org.example.steps.ArithSteps.iAddAnd(int,int)
Then I get 4 # org.example.steps.ArithSteps.iGet(int)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.253 s - in org.example.steps.CucumberTestRunner
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.815 s
[INFO] Finished at: 2020-11-05T10:51:50-08:00
[INFO] ------------------------------------------------------------------------
C:\Maven\ArchCuke\untitled>
This is my runner class
package org.example.steps;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"classpath:features"},
plugin = {"pretty"}
)
public class CucumberTestRunner {
}
and this is my 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>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<name>untitled</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.version>4.13</junit.version>
<cucumber.version>6.8.0</cucumber.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
<maven.surefire.version>2.22.2</maven.surefire.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<!-- Include our runner class or 'mvn test' won't work; name doesn't match default template.
See https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html -->
<configuration>
<includes>
<include>CucumberTestRunner.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I tried version 3.0.0-M5 of the surefire plugin instead of 2.22.2, with the same results.
What am I doing wrong here?
The cucumber.options property was deprecated and removed. You have to pass each option as a single property.
mvn test -Dcucumber.filter.tags='#smoke and not #ignore'
https://github.com/cucumber/cucumber-jvm/blob/main/release-notes/v5.0.0.md#property-based-options
You should not use the system property directly in your CLI. You should deliver the system property to the surefire subprocess
mvn test "-DargLine=-Dcucumber.filter.tags='#smoke and not #ignore'"
or make a little trick in your POM and then use your original CLI:
<properties>
<argLine>-Dcucumber.filter.tags='${cucumber.filter.tags}'</argLine>
</properties>

Unable to configure maven to find antlr4 generated-sources files

I'm trying to figure out how to use Maven with antlr4, and haven't been able to find anything that quite works all the way through. I'm no Maven expert. I'm starting out with a scaled-down example from chapter 3 of "The Definitive ANTLR 4 Reference". I'm using the Command Prompt console window on Windows 10. I was originally trying to use Eclipse but wasn't having much luck, so I thought I'd go back to basics and try the Command Prompt.
My main java file called App.java is at my-app/src/main/java/com/mycompany/app/App.java
My antlr4 grammar file called ArrayInit.g4 is at my-app/src/main/antlr4/ArrayInit.g4
The java files generated from ArrayInit.g4 (ArrayInitListener.java, ArrayInitParser.java, etc.) after typing "mvn antrl4:antlr4" are at my-app/target/generated-sources/antlr4
My question for the moment is how can I set up my pom.xml file so that when I compile App.java, it compiles and includes the generated antlr4 java files. I've been trying to configure the pom with various source, output, destination directories, but with no luck.
This is my grammar file ArrayInit.g4 -
grammar ArrayInit;
init : '{' value (',' value)* '}' ;
value : init
| INT
;
INT : [0-9]+ ;
WS : [ \t\r\n]+ -> skip ;
This is my main java file App.java -
package com.mycompany.app;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class App
{
public static void main( String[] args ) throws Exception
{
ANTLRInputStream input = new ANTLRInputStream(System.in);
ArrayInitLexer lexer = new ArrayInitLexer(input);
}
}
This is my pom.xml file -
<?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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7.2</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
This is my error message when I try to run mvn compile -
E:\mvnbael\my-app>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # my-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\mvnbael\my-app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) # my-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\mvnbael\my-app\target\classes
[INFO] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java: E:\mvnbael\my-app\src\main\java\com\mycompany\app\App.java uses or overrides a deprecated API.
[INFO] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java: Recompile with -Xlint:deprecation for details.
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,17] cannot find symbol
symbol: class ArrayInitLexer
location: class com.mycompany.app.App
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,44] cannot find symbol
symbol: class ArrayInitLexer
location: class com.mycompany.app.App
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.240 s
[INFO] Finished at: 2019-09-13T11:07:21-07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project my-app: Compilation failure: Compilation failure:
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,17] cannot find symbol
[ERROR] symbol: class ArrayInitLexer
[ERROR] location: class com.mycompany.app.App
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,44] cannot find symbol
[ERROR] symbol: class ArrayInitLexer
[ERROR] location: class com.mycompany.app.App
As the comment says, I had to remove <pluginManagement> tags from the pom.xml in order to get Maven to automatically run ANTLR as part of generate-sources.

tests are not run during maven test procedure from IDE (using surefire)

In my Spring app I have tests named properly:
src
-main
-test
-java
-com.mypackage.sth
-utils
SomethingTest.java
I can run it straight from the IDE intellij, but when I chose it from maven window:
I'm getting info:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
in my pom.xml file I have this section:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
I also have a dependency added above:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
The test still don't run though. How can I fix it? What's the main problem here?
----- EDIT
I did exactly what Naor Tedgi suggested in the first answer, however right after adding:
<sourceDirectory>src/main/..</sourceDirectory>
<testSourceDirectory>src/test/..</testSourceDirectory>
to the <build> tag, my project structure went nuts, after doing so it looks like this:
src
-main
-test.java.com.mypackage.sth
-utils
SomethingTest.java
and all the symbols in test classes are not resolvable. Now when I try to run tests, they run! so that's a good thing, but there are lots and lots of errors, e.g.
package org.junit does not exist
etc.
--- EDIT v2
okay, I edited my pom.xml so that now it contains these two lines in <build>:
<sourceDirectory>src/main/java/</sourceDirectory>
<testSourceDirectory>src/test/java/</testSourceDirectory>
errors disappeared and the project is compilable, however it still didn't help - the tests did not run
Here's the output from mvn test:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myProject 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) # projectName ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) # projectName ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) # projectName ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\path\to\myProject\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) # projectName ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) # projectName ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.181 s
[INFO] Finished at: 2019-02-27T17:16:29+01:00
[INFO] Final Memory: 22M/224M
[INFO] ------------------------------------------------------------------------
first, extract Junit outside of build tag into dependencies
then add this lines to build
<sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
<testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>
should look something like this
<project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
<testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>
<plugins>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>

TestNG tests are not getting run when I execute testng.xml using POM file using maven-surefire

0 tests are getting run, when I execute testng.xml using POM file using Maven-surefire.
I am running few selenium tests using testng.xml.
When I run testng.xml file as TestNG test Suite, then it runs fine.
But, when I include testng.xml file (as below) then its not running :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
Below is my POM file snippet:
<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>SAPAutomation</groupId>
<artifactId>SAPAutomation</artifactId>
<!-- <version>3.2</version> -->
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SAPAutomation</name>
<url>http://maven.apache.org</url>
<!-- For javadocs -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.17</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- To exclude unwanted Package ex: com.SAPAutomation.TEMP etc-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<exclude>**/com/SAPAutomation/Temp/*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.15</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.codeartisans.thirdparties.swing</groupId>
<artifactId>org-openide-util</artifactId>
<version>8.6.2</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14-10.2.0.4.0</artifactId>
<version>10.2.0.4.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/lib/ojdbc14.jar</systemPath>
<!-- <systemPath>${java.home}/lib/ojdbc14.jar</systemPath> -->
</dependency>
<dependency>
<groupId>org.monte.screenrecorder</groupId>
<artifactId>MonteScreenRecorder</artifactId>
<version>10</version>
<scope>system</scope>
<systemPath>${basedir}/src/lib/MonteScreenRecorder.jar</systemPath>
<!-- <systemPath>${java.home}/lib/ojdbc14.jar</systemPath> -->
</dependency>
<!-- <repositories>
<repository>
<id>ojdbc14</id>
<url>http://www.oracle.com/technetwork/apps-tech/jdbc-10201-088211.html</url>
</repository>
</repositories> -->
</dependencies>
</project>
My testng.xml file:
<suite name="Suite" parallel="tests">
<listeners>
<listener class-name="com.SAPAutomation.Listners.RetryListener"/>
</listeners>
<test name="Test1">
<parameter name="TestCaseId" value="TC1" />
<classes>
<class name="com.xyz.abc.MyAutomationTestByXML"/>
</classes>
</test>
<test name="Test2">
<parameter name="TestCaseId" value="TC2" />
<classes>
<class name="com.xyz.abc.MyAutomationTestByXML"/>
</classes>
</test>
<test name="Test3">
<parameter name="TestCaseId" value="TC3" />
<classes>
<class name="com.xyz.abc.MyAutomationTestByXML"/>
</classes>
</test>
</suite>
Test result of running Pom.xml:
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for SAPAutomation:SAPAutomation:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.oracle:ojdbc14-10.2.0.4.0:jar should not point at files within the project directory, ${basedir}/src/lib/ojdbc14.jar will be unresolvable by dependent projects # line 153, column 16
[WARNING] 'dependencies.dependency.systemPath' for org.monte.screenrecorder:MonteScreenRecorder:jar should not point at files within the project directory, ${basedir}/src/lib/MonteScreenRecorder.jar will be unresolvable by dependent projects # line 162, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SAPAutomation 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\AutomationWorkSpace\SAPAutomation\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) # SAPAutomation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) # SAPAutomation ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 35 source files to C:\AutomationWorkSpace\SAPAutomation\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.14.1:test (default-test) # SAPAutomation ---
[INFO] Surefire report directory: C:\AutomationWorkSpace\SAPAutomation\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.496 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.137 s
[INFO] Finished at: 2015-04-09T15:01:01+00:00
[INFO] Final Memory: 24M/277M
[INFO] ------------------------------------------------------------------------
Kindly advise.
Issue still persists, even after giving full path of Testng.xml in my POM.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for SAPAutomation:SAPAutomation:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.oracle:ojdbc14-10.2.0.4.0:jar should not point at files within the project directory, ${basedir}/src/lib/ojdbc14.jar will be unresolvable by dependent projects # line 154, column 16
[WARNING] 'dependencies.dependency.systemPath' for org.monte.screenrecorder:MonteScreenRecorder:jar should not point at files within the project directory, ${basedir}/src/lib/MonteScreenRecorder.jar will be unresolvable by dependent projects # line 163, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SAPAutomation 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\AutomationWorkSpace\SAPAutomation\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) # SAPAutomation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # SAPAutomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) # SAPAutomation ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 35 source files to C:\AutomationWorkSpace\SAPAutomation\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.14.1:test (default-test) # SAPAutomation ---
[INFO] Surefire report directory: C:\AutomationWorkSpace\SAPAutomation\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.501 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.402 s
[INFO] Finished at: 2015-04-14T17:31:11+00:00
[INFO] Final Memory: 22M/167M
[INFO] ------------------------------------------------------------------------
You can place your testng.xml file under project directly with your pom file then it will execute your test suite.
You need to provide the full path to the testng.xml file for Maven to pick it up.
I was facing this same scenario (issue) while doing the Maven with TestNG integration hands-on along with Rahul Sir's video.
Without the TestNG.xml, POM.xml was able to execute all the tests while being invoked using "mvn test" command from the CLI.
However, I noticed, if I just run the test directly from testng.xml i.e. "run as TestNG suite", then none of the test cases were run.
While took a close look into my test java classes, I discovered that, for using the #Test annotations before my test cases (methods), the import library was --> import org.junit.Test; Because even jUnit has a Test class in its library.
But since we are using TestNG, thus we must need to use this import --> import org.testng.annotations.Test;
While we create a Maven project within Eclipse using the project template i.e. maven-archetype-quickstart template, it creates two parallel folder structure -> one for java (to have the object models & utilities) and the other for test (to contain the test cases). So, by default, there is AppTest.java class and that uses jUnit library for #Test annotation.
So, right after creating the Maven project in eclipse, we should add the TestNG dependency in the pom.xml in case we know that we'll be using TestNG for execution.
I am able to have this issue resolved just by using import org.testng.annotations.Test; within each of my test classes.
TestNG SuiteXML will only work on those #Test annotations while org.testng.annotations.Test is present for the classess.
Hope this will resolve your error too. Thank you.
In the maven-surefire-plugin, provide the full path from under your project to your testng.xml file -- Example:
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
Above issue got resolved using updating version of Maven-Surefire-plugin in the mentioned POM.xml. Whole set-up lived Happily ever after & after!!
Thanks

Resources