Encoding in Maven Tests not working properly - java

I am currently trying to write a test that is run via Maven that specifically addresses strings containing UTF-8 characters. If I run said test in IntelliJ everything is fine and the results are as expected. If i run the test using mvn test, then (only) the test that is testing UTF-8 characters is failing.
This is my test:
#Test
public void testWithUTF8() throws InvalidKeyException, NoSuchAlgorithmException {
String signature = NFLAuth.sign("Contains UTF-8: äüöööÕßÍÑð");
Assert.assertEquals("Signature=BSY4prbinpAgzJLv6ffGm+XJb1NTIbGY6gTj8RA3lsA=", signature);
}
First of all, yes, I read the question about encoding in Maven and I did everything. Theres the property, its added to the compiler plugin, I have even set JAVA_TOOL_OPTIONS with file.encoding but still no luck. I also dont know what encoding it is using, if I try windows-1252 in IntelliJ the test also fails, but the signature is not the same as maven gets.
Here is the POM:
<?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>
<artifactId>***</artifactId>
<version>1.0</version>
<name>***</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.apigee.edge.4g</groupId>
<artifactId>expressions</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.apigee.edge.4g</groupId>
<artifactId>message-flow</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.apigee.edge.4g</groupId>
<artifactId>kernel</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/kernel-api-1.0.0.jar</systemPath>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>

The maven surefire plugin also needs to read the files as UTF-8 otherwise it wont do it :). I had tried something like this before but with the wrong Element in <configuration/>...
Proper configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>

Set (Windows) environment variable JAVA_TOOL_OPTIONS to -Dfile.encoding=UTF8
The other answer (set encoding to Surefire config) did not solve my issue.
After setting JAVA_TOOL_OPTIONS, no surefire configuration is needed.

I had to only set two Windows environment variables:
JAVA_TOOL_OPTIONS to -Dfile.encoding=UTF8 (thanks to #joro)
MAVEN_OPTS to -Dfile.encoding=UTF8

Related

Maven can't resolve any Java import statements

Now I'm using IntelliJ IDEA 2019.3.3 (Community Edition) with Maven 3.6.1., JDK 1.8.0_131 at a laptop with Windows 10.
I imported the project from GitHub, that 100% works at other PCs.
For the unknown reason can't resolve any import:
When I trying import any class I receive a message like Cannot resolve symbol 'WebDriver'.
The same situation was with a project created from scratch.
I tried to re-install IntelliJ IDEA (current and previous versions), Maven (current and previous), JDK (13). Also, I tried to delete .M2 directory, re-import Maven project but still had this issue.
When I tried to compile (or execute any commands, like "install", "deploy" etc.) project from Maven command line I received:
[WARNING] Error injecting: org.apache.maven.plugin.resources.ResourcesMojo
com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) No implementation for org.apache.maven.shared.filtering.MavenResourcesFiltering was bound.
while locating org.apache.maven.plugin.resources.ResourcesMojo
1 error
Project JDK is set up properly.
Project's POM.xml is:
<?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.udc</groupId>
<artifactId>serenity-selenium-10.02.2020</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<serenity.version>2.1.6</serenity.version>
<jbehave.version>1.46.0</jbehave.version>
<selenium.version>3.141.59</selenium.version>
<webdrivermanager.version>3.8.1</webdrivermanager.version>
<mysql.version>8.0.19</mysql.version>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<surefire.version>2.20</surefire.version>
<source.encoding>UTF-8</source.encoding>
<java.version>8</java.version>
<test.name>*</test.name>
</properties>
<dependencies>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>${serenity.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>${serenity.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-jbehave</artifactId>
<version>${jbehave.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${source.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<includes>
<include>**/tests/**/${test.name}.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>net.serenity-bdd.maven.plugins</groupId>
<artifactId>serenity-maven-plugin</artifactId>
<version>${serenity.version}</version>
<executions>
<execution>
<id>serenity-reports</id>
<phase>post-integration-test</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Java Maven dependency inheritance

I created maven artifact, that have dependencyies on gson and ok3http.
The pom.file
<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.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
<build>
<finalName>testConnect</finalName>
<plugins>
<!-- download source code in Eclipse-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Copy project dependency -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now I send it to local repository (later will send to Maven central) and I want people to be able to use my artifact in their pom files like:
<dependency>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
</dependency>
It builds ok, but when I try to use it, it throws error that it can not found gson and ok3http.
If I add also:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
It started to work. Is it possible that people can add dependency to my artifact only and other dependencies will add automatically or something like this?? Thanks!
Although will be very thankful if you give me the right documentations how to send my code to maven repo.

Why Maven is not running test cases?

I am beginner in maven and I have created a maven project, when I execute the project via clicking on as Maven Test it doesn't execute any test cases which is written under the #Test annotation.
But same code when I execute with Testng then its work fine.
Can anyone give some light to resolving it.
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>Webdriver</groupId>
<artifactId>Webdriver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</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.19</version>
<inherited>true</inherited>
<configuration>
<suiteXMLFiles>
<suiteXMLFile>testng.xml</suiteXMLFile>
</suiteXMLFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
By convention, the test classes must also end with "Test". If they don't, you should add
<includes>
<include>**/*.java</include>
</includes>
to your surefire config (or whatever includes work for you and your file naming scheme).

Running groovy tests with Maven

I have a folder structure as below
ProjectName
- src
- test
- groovy
- java
When I run command mvn clean install . It only runs Java Unit tests(Located under *src\test\java* directory) while Groovy tests cases are not invoked. It seems Groovy is not finding Groovy tests anymore.
Please help me in enabling groovy tests cases Execution with the help of Maven ?
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ApacheActiveMQSpike</groupId>
<artifactId>ApacheActiveMQSpike</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ApacheActiveMQSpike</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.8.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.6-beta-2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You will need to use gmaven plugin to trigger groovy tests
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0-rc-3</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<sources>
<fileset>
<directory>${pom.basedir}/src/test/java</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</fileset>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Nice article about running Groovy tests in Maven.
The structure should be
<project>
...
<build>
<plugins>
<plugin>
....
</plugin>
</plugins>
</build>
...
</project>

Package missing on maven build

I'm trying to run a JUnit test in Travis CI using Maven, but every attempt makes me face the same error:
[ERROR] \luiz.kill\Workspace\hawickjames\src\main\java\com\lzkill\main\CountElementaryCircuitsFromRFC.java:[3,24] error: package org.jgrapht.graph does not exist
I also see this when I use m2e to fire Maven, the dependencies don't even show up Eclipse as if the download was not processed.
It really looks like a javac problem. How can I make the download succeed and the compiler find the right jars?
Here's my 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.lzkill</groupId>
<artifactId>myArtifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>...</name>
<description>...</description>
<url>...</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<show>protected</show>
<windowtitle>...</windowtitle>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
You did not specify any dependencies only which version to use if you declare a dependency.
Remove the the dependencyManagement tags and you are set.
DependencyManagement helps you resolve version conflicts and is a good idea in a parent pom of multiple related projects. But it does not declare a dependency.

Categories

Resources