Maven Compilation error [package org.testng.annotations does not exist] - maven

I'm pretty new to maven and I want to run my test classes using maven. I have generated the testng.xml and I have created the POM.xml file also. But when you run the mvn install, it generates this error :
[package org.testng.annotations does not exist]
please advice on this.
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>com.TestNG</groupId>
<artifactId>TestNG</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="1" preserve-order="true">
<test name="Test">
<packages>
<package name="com.testngTest2" />
<package name="com.testngTest" />
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->

I've got similar problem. The reason for that was "Scope" option of "testng" dependency set to "test" when "compile" was needed.
How I fixed it (note, I used Eclipse):
Open pom.xml file.
Go to "Dependencies" tab.
Select "testng" package and click on "Properties..."
On opened screen change "Scope" option to "compile" and click "OK" to save it.
Try to build your project again with "compile test" goals.

remove test scope testng dependency and add compile
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>compile</scope>
</dependency>

In your pom.xml file you have scope of testng as test
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
<scope>test</scope>
</dependency>
Replace the scope by compile
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
<scope>compile</scope>
</dependency>

Beleive me below wil work replace "test" to "compile" in scope
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
<scope>compile</scope>
</dependency>

Even I had faced this issue and got a solution for the same.
In your pom.xml file remove the scope and this should work fine.
As per below code in pom.xml, remove the scope tag.
Even though we have imported the maven dependency for Testng, when you add scope tag in XML file, it treats as JUnit annotation and not as Testng. So when I removed scope tag, My #Test Annotation was treated as Testng Annotation.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.7</version>
**<scope>test</scope>** //Remove this line and compile maven
</dependency>

test to compile -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>compile</scope>
</dependency>

Make sure your tests are in "src/test/java".
It will solve this problem

Set testng's scope to "provided" if you're building a web app and don't want it included in WEB-INF/lib.

Below solution works for me-
Please add below dependencies in your pom.xml
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>

For JetBrains IntelliJ users who can't find pom.xml file:
go to 'file' > 'project structure'
select module where test package is located from 'project settings' > 'modules'
go to dependencies tab
change scope setting from 'test' to 'compile'

Related

Maven project build fails in IntelliJ when annotation processors are used (google/auto-value)

I use google/auto-value to create immutable value classes in a maven project.
<?xml version="1.0" encoding="UTF-8"?>
<project 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"
xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
[...]
<packaging>war</packaging>
<properties>
<auto-value.version>1.7</auto-value.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>${auto-value.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>${auto-value.version}</version>
</dependency>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
This works like a charm using the CLI (e.g. mvn clean test) but creates an error during IntelliJ project builds:
Error:java: java.lang.NoClassDefFoundError: com/google/auto/service/AutoService
com.google.auto.service.AutoService
Noteworthy: The correct sources are generated into generated-sources/annotations/... but the IntelliJ build fails after this step and does not create the generated test sources directory generated-test-sources/....
While the issue can be easily fixed by adding another annotation processor path to the maven-compiler-plugin
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0-rc6</version>
</path>
this fix has the downside of looking up and manually changing the auto-service version whenever the auto-value-dependency version changes. Is there an obvious mistake i made in my pom file or a setting in IntelliJ i don't know? As far as i can see a correct annotation processing profile is created when i import the project into IntelliJ.
I faced the same issue, and I fixed it without touching to the code. Here's what I did:
In the Settings/Preferences dialog Ctrl+Alt+S, go to Build, Execution, Deployment | Compiler | Annotation Processors.
Select the default, or select your own application profile or create a new one (click "+" on the bottom of the page).
Make sure Enable annotation processing is selected
Change the radio button from Processor path to Obtain processors from the project classpath.
This looks like a bug in IntelliJ, if it builds with mvn but not from within IntelliJ. I see the same thing. There is an alternative way of configuring AutoValue which avoids the problem:
<dependencies>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>1.7</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.7</version>
<optional>true</optional>
</dependency>
</dependencies>
You don't need the <annotationProcessorPaths> stuff in this case. On the downside, there's apparently some risk of the AutoValue annotation processor (the auto-value artifact) or its dependencies finding their way into your built project.

"Cannot access com.google.common.base.Predicate" in Selenide

I am new to Selenide and I tried to follow this video: https://vimeo.com/107647158 at 8:05 - part
$("#ires li.g").shouldHave
is underlined with "Cannot access com.google.common.base.Predicate" error.
I tried some Google and found this: What is the fix for class file for com.google.common.base.predicate not found? which pointed to https://github.com/google/guava page.
I added dependency to pom:
<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>com.company.myFirstSelenideTest</groupId>
<artifactId>myFirstSelenideTest</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>metadataTestonDPU</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>4.12.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_131\bin\javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
But it didn't resolve my issue.
I also checked %JAVA_HOME% and path to JDK/JRE and seems to be fine. What I missed?
Oh, duck method...
I added import com.google.common.base.Predicate; on the top and IntelliJ IDEA posted on red Predicate part with a tip to add it to a classpath
Sorry for dumb question
I faced same issue for Predicate & Collect, then added below and it worked
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-base</artifactId>
<version>r03</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-collections</artifactId>
<version>r03</version>
</dependency>
Springfox 3.x removes dependencies on guava and other 3rd party libraries (not zero dep yet! depends on spring plugin and open api libraries for annotations and models) so if you used guava predicates/functions those will need to transition to java 8 function interfaces

Maven compile error [package org.testng.asserts does not exist]

When I try to build project via console by [mvn clean install -DskipTests] I get error. I use in my tests testNG SoftAssert and in a test class I just added an import import org.testng.asserts.SoftAssert but looks like maven does not see that package.
Error from console:
package org.testng.asserts does not exist
My pom.xml looks like
<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.atlassian</groupId>
<artifactId>tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>confluence</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
Such errors occur when corresponding dependency version do not have the classes you are trying to use. In this case the TestNG version 6.1.1 you are using, does not have package org.testng.asserts. Try using below version,
Also, it will not give error for SoftAsserts import, if you have asked IDE to include TestNG library. This TestNG library surely is of higher version than the one you are referring from pom.xml. Try to keep same versions both in pom.xml & your IDE's testNG plugin to avoid such varying behavior.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
</dependency>
Above version is surely working. Give it a try.
I found out that removing scope inside testng dependency worked. I tried running with scope added to the same dependency but failed. Strange but it just worked by removing testng scope dependency.
Tried different versions, but it did not help. Removing a scope from dependency indeed solved the issue.

Grizzly 2.3.11 - CLStaticHttpHandler - cannot read index.html under resource folder when packed in a jar

I want to host static web pages in a jar. So I used Maven to pack the java project containing a folder having a index.html web page. My code:
server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, start);
server.getServerConfiguration().addHttpHandler();new CLStaticHttpHandler(Server.class.getClassLoader(), myfolder/), /mysite)
When I access http://localhost:8080/mysite/ in IDE, the handler is able to read index.html. But if I use mvn package and run the jar file, http://localhost:8080/mysite/ doesn't work, unless I specify http://localhost:8080/mysite/index.html in a browser to make it work. The web page folder is under src/main/resources, and it is under the root path when opening the jar.
Thank you so much!
Added: To reproduce this, you can create a Maven project by writing a pom.xml and put something like
<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>com.example</groupId>
<artifactId>simple-service</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-service</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
<scope>compile</scope>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.6</jersey.version>
<grizzly.version>2.3.11</grizzly.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
and create a server like:
final ResourceConfig rc = new ResourceConfig();
rc.register(new LoggingFilter(Logger.getLogger(Main.class.getName()), true));
return GrizzlyHttpServerFactory.createHttpServer(new URI(BASE_URI), rc);
server.getServerConfiguration()
.addHttpHandler(new CLStaticHttpHandler(ServletSimple.class.getClassLoader(), "statichtmlfolder/"), "/ui/" );
System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.shutdown();
statichtmlfolder is a folder containing all the index.html file under /src/main/resources/. we are using Jersey2 here. And use mvn package to package the code to a jar file, go to target folder, then run java -cp dependency/*:api-server-1.0.26-SNAPSHOT.jar com.example.Main. We can see the statichtmlfolder is under the root directory in the jar file.
The bug is fixed in Grizzly 2.3.13
https://java.net/jira/browse/GRIZZLY-1687

Set up Maven in Intellij with TestNG using POM to read .xml suite

Hey I have my project set up with Maven structure:
My pom file looks like:
<?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.QASelenium</groupId>
<artifactId>MyTemp</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/testing.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- <scope>test</scope> -->
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<!-- <scope>test</scope> -->
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.31.0</version>
</dependency>
</dependencies>
</project>
My TestNGTest.java file is under src/main/resources:
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestNGTest{
#Test
#Parameters(value="number")
public void parameterIntTest(int number) {
System.out.println("Parameterized Number is : " + number);
}
}
When I compile it with TestNG using IntelliJ, WHy I always get this error:
org.testng.TestNGException:
Parameter 'number' is required by #Test on method parameterIntTest but has not been marked #Optional or defined
in C:\Users(My_Name).IdeaIC12\system\temp-testng-customsuite.xml
Can someone please point to me where I got it running wrong?
I am new to the maven, Thank you so much in advance.
I think all is fine, but if you are using xml suite, then you need to execute test from suite, not from class.
Run/Debug Configurations
Configuration -> Suite
Suite: /PATH/PROJECT/src/test/resources/testng.xml

Resources