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

时间:2016-08-15 08:20:14

标签: java maven encoding utf-8

我目前正在尝试编写一个通过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);
}

首先,是的,我在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>

2 个答案:

答案 0 :(得分:4)

maven surefire插件还需要将文件读取为UTF-8,否则它不会这样做:)。我之前尝试过这样的事情,但在<configuration/> ...

中使用了错误的元素

- &GT;溶液

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
    </configuration>
</plugin>

答案 1 :(得分:0)

不要忘记将<?xml version="1.0" encoding="UTF-8"?>放在.m2\settings.xml的第一行