19

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>
6
  • Post your POM. Are you sure you're using the right encoding for the compilation step? Commented Aug 15, 2016 at 8:48
  • Are you sure you added in your pom a property like this <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ? If not, try to do this
    – Prim
    Commented Aug 15, 2016 at 8:52
  • I have added the POM Commented Aug 15, 2016 at 10:39
  • Try first u-escaped text, so as to check that the editor encoding equals the javac encoding: "\u00E4\u00F6\u00FC..." (äöü...). Especially as Windows-1252 would cover the letters too.
    – Joop Eggen
    Commented Aug 15, 2016 at 10:53
  • Good idea, I did the following: NFLAuth.sign("\u00E4\u00F6\u00FC"); and within that method I printed the getBytes() of the input variable. IntelliJ: C3 A4 C3 B6 C3 BC Maven: E4 F6 FC So apparently it is not using UTF-8 encoding in Maven... Commented Aug 15, 2016 at 11:01

3 Answers 3

36

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>
2
  • 2
    Thanks, this solved an issue I was having too. Still it is weird: the above test - and mine - doesn't read test data from a file system. It is embedded in the code file of the test, so I would expect only the project.build.sourceEncoding property to have any effect.
    – Kristiaan
    Commented Dec 1, 2020 at 16:06
  • Many things in maven seem like they were finally done but are repeated in other stages. It might look your file was compiled with UTF-8 but for tests it is recompiled with Windows -1252 or so... Commented Dec 2, 2020 at 23:44
3

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.

1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.