Maven测试中的编码无法正常工作

Yan*_*ann 4 java encoding utf-8 maven

我目前正在尝试编写一个通过Maven运行的测试,该测试专门针对包含UTF-8字符的字符串。如果我在IntelliJ中运行所说的测试,一切都很好,并且结果符合预期。如果我使用mvn test运行测试,则(仅)正在测试UTF-8字符的测试失败。

这是我的测试:

@Test
public void testWithUTF8() throws InvalidKeyException, NoSuchAlgorithmException {
    String signature = NFLAuth.sign("Contains UTF-8: äüöööÕßÍÑð");
    Assert.assertEquals("Signature=BSY4prbinpAgzJLv6ffGm+XJb1NTIbGY6gTj8RA3lsA=", signature);
}
Run Code Online (Sandbox Code Playgroud)

首先,是的,我阅读了有关在Maven中进行编码的问题,然后我做了一切。Theres属性,将其添加到编译器插件中,我什至用file.encoding设置了JAVA_TOOL_OPTIONS,但还是没有运气。我还不知道它使用的是什么编码,如果我在IntelliJ中尝试Windows-1252,则测试也会失败,但是签名与maven获得的签名不同。

这是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>
Run Code Online (Sandbox Code Playgroud)

Yan*_*ann 6

Maven surefire插件还需要以UTF-8格式读取文件,否则它将不会执行:)。我之前曾经尝试过类似的事情,但是在<configuration/>...中使用了错误的元素

->解决方案

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这解决了我也遇到的问题。但还是很奇怪:上面的测试 - 和我的 - 没有从文件系统读取测试数据。它嵌入在测试的代码文件中,因此我希望只有 project.build.sourceEncoding 属性具有任何效果。 (2认同)