I have set up a maven project with Junit tests to run Selenium (java) scripts on our build server. From what I read Maven is meant to run any files specified in the folder test.java
With this in mind I set up my project with a TestAll class using#RunWith and #SuiteClasses to run specifically the classes and tests I wanted.
However, when I run mvn test maven runs all methods that have the keyword test anywhere in the title, ie Test_A_1423, all tests in a class with "Test" in the title and everything in any package with the keyword "Test". How do I run only the tests that I want or do I need to remove "Test" from any class or method I dont want to run. Included is my pom.xml file
`
<modelVersion>4.0.0</modelVersion>
<groupId>cyberx.automation</groupId>
<artifactId>tests</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cyberx.test.automation</name>
<url>http://maven.apache.org</url>
<!--<repositories>-->
<!--<repository>-->
<!--<id>central</id>-->
<!--<name>bintray</name>-->
<!--<url>http://jcenter.bintray.com</url>-->
<!--</repository>-->
<!--</repositories>-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.testng</groupId>-->
<!--<artifactId>testng</artifactId>-->
<!--<version>6.9.4</version>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
`
You have regular expression to include/exclude tests.
See here for documentation of the surefire plugin.
Related
Please provide steps to how to install surefire plugin in Maven. I have googled and found no specific answer. There were lots of answer about the solution but no specific answer on how to install the plugin with steps.
Excpetion I am getting:
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator#2f410acf
pom.xml i use :
<!-- language: 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>au.com.simcentric</groupId>
<artifactId>Example20</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.14.0</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.14.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.14.0</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jvnet.hudson.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1-hudson-3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>3.14.0</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hellojavaer/poi-excel-utils -->
<dependency>
<groupId>org.hellojavaer</groupId>
<artifactId>poi-excel-utils</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.1.13</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-java-commons</artifactId>
<version>2.0-BETA19</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<version>2.44.0</version>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<scope>test</scope>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.14.0</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertx.version>3.0.0-milestone5</vertx.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Referring to the attached pom.xml file, it is evident that inclusion of the surefire plugin is missing. Maven needs to know what plugin to include and which version. The following is suggested:
Add the Maven Surefire plugin entry in the pom.xml file
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>for example: 2.22.0</version>
</plugin>
</plugins>
</pluginManagement>
Then run mvn test
The usage example above has been extracted from here
Going a bit further, you can also include/exclude tests using the configuration schema
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>for example: 2.22.0</version>
<configuration>
<includes>
<include>IncludeTest.java</include>
</includes>
<excludes>
<exclude>ExcludeTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement></build>
A detailed instruction with examples is shown here.
Incase you would like surefire to hook to your testng.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
Upon success, the surefire plugin will generate a XML report at target/reports inside your project folder.
After so many researchers i have found the solution for my problem. in first place i thought there is a problem in my pom.xml file. but after make sure my pom.xml is correct i figure out system does not displays/recognized the maven in my machine. (mvn test) .
As the solution i followed below steps and now the system doesn't give me the exception i mentioned above in my question .
open cmd and type mvn test (in my case system didnt recognized the mvn)
Navigate to path > edit the system variables> Environment variables >select system variable- path
enter image description here
3.click on edit button and add the machine path which maven is available in to the end of the available path .
enter image description here
4.click ok
5.open cmd
6.type cmd test <>
7.now you can run your Maven project without the exception i have mentioned in my question :)
I tried cleaning my project through maven->clean and project->clean but it didnot work. I tried changing my output folders explicitly under build path but not sure what to select for src/test/java
I also updated my testng plugin in pom.xml
pom.xml
<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>abc</groupId>
<artifactId>xyz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Test</description>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-ie-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-ie-driver</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Output folders for src/test/java and src/test/resources is /target/classes
and I am getting TestNG class not found in classpath error :
org.testng.TestNGException:
Cannot find class in classpath: testclass file
Earlier this code was working fine. I just tried refractoring the project by changing artifact id of maven project and from then not able to proceed.
Not sure how widely this applies, but I had success with this:
Right click > Java build path > Source
Make sure Include:(All) and Exclude:(None) is checked for all 4 folders
src/main/java
src/main/resources
src/test/java
src/test/resources
You need to add maven surefire plugin to run testNG classes. It's a miracle if you were able to run the tests earlier without this plugin. You have to add below plugin in the build phase to get going.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>Sample.java</include>
</includes>
</configuration>
</plugin>
If you don't want to include the tests manually then by default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
"**/Test*.java"
"**/*Test.java"
"**/*Tests.java"
"**/*TestCase.java"
I think you have to convert non-maven project to maven project
right click on project->Configure->Convert it to Maven
The issue for me was that the "Skip Tests" checkbox was checked.
RClick project -> Run As -> Run Configurations -> Maven Build (select your build configuration) -> Uncheck Skip Tests checkbox
This checkbox prevents Maven from compiling tests
If you want to compile the tests, but don't want to execute them during build process, add -DskipTests in Goals
I am building a web project which uses Maven for building and uses Jersey for RESTful API. I have written unit tests using TestNG and Jersey Test Framework and am running them in-memory and not on a web server.
In my tests I make a JDBC call to my MySQL database and get some results. The tests run fine when I run from within Eclipse using the TestNG for Eclipse plugin. However, when I try running from Maven using either of the two commands:
mvn clean package
or
mvn test
I get the output as shown below:
My pom.xml is as follows:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>co.teamlink.services</groupId>
<artifactId>teamlink-services</artifactId>
<packaging>war</packaging>
<version>0.5.0</version>
<name>teamlink-services</name>
<build>
<finalName>teamlink-services-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
<scope>runtime, test</scope>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.25.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Your help is appreciated as this is driving me crazy. I am not sure what am I missing exactly.
There are several issues with your pom.xml file.
First the scope for mysql-connector-java in dependencyManagement section is defined incorrectly:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
<scope>runtime, test</scope>
</dependency>
Maven doesn't allow defining comma separated list of scopes. In your case it is enough to specify runtime scope as then library will be available during test execution and runtime. So it should be
<scope>runtime</scope>
Next dependencyManagement section is used only to declare "standard" usage of the dependency in project and it's child projects. To actually include the library in you project it should be listed in project dependencies. And your pom lacks
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
among project dependencies.
I want to convert to Maven a lot of java projects. I've tried to do: Right click on my project --> Configure --> Convert to Maven Project. but I only get: pom.xml (I put every depency that I need, plugins and etc).
My pom.xml contains:
<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>prueba</groupId>
<artifactId>prueba</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<type>maven-plugin</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.primefaces/primefaces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
</dependencies>
</project>
But my java docs appears empty and the following code structure inside:
Structure
Content
But as you can see, it doesn't work because I don't have my original java code in "Maven format" and it doesn't build anything in the test.
How could I convert every java file to Maven to can execute in my TestNG.xml??
Thanks so much!!!
It seems your dependencies are not resolved or the project is not built.
Can you right click on your pom.xml and run maven install. Is your build successfull ?
Once your build is successfull.
Can you right click on project -> Maven -> Update project -> Check the Force update of snapshots/releases.
This will get all the dependencies in your classpath.
Build the code again and then it should work.
Best Regards,
Saurav
In keeping with the m2e bug/issue, I followed the solution mentioned #SL4j m2e fails to load in Eclipse IDE.
My Pom.xml looks like this (for my JUnit and Selenium dependencies):
<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>TestJUnit</groupId>
<artifactId>TestJUnit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.41.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.41.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
However when I do a mvn compile I get the following kind of error:
[ERROR] /path:/to/file/<foldername>/<JUnitClassName>.java:[9,17] package org.junit does
not exist
I am new to maven, is there something that I am doing wrong here.
In your <dependencies> list, you specified JUnit with the scope of test, which means that it is only included in the jar when you do mvn test, but not included in mvn compile. If you want JUnit to be included in your final jar, change the <scope>test</scope> to <scope>compile</scope> or you can jsut remove the tine, they both do the same.
TL;DR use mvn test to test your application; or change <scope>test</scope> to <scope>compile</scope> in the junit dependeny decleration.
I did the same thing when I first started using maven, almost everyone does :P