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

标签 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 中编码的问题,我做了所有的事情。有这个属性,它被添加到编译器插件中,我什至用 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>

最佳答案

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>

关于java - Maven 测试中的编码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38951630/

相关文章:

java - 如何在 Quarkus 中启用 Jaeger JDBC 跟踪

java - 在 Java 中更新 ArrayList 中的元素?

java - 从约束布局循环中设置 View 子项的重力

sqlite - 修复 sqlite 编码

java - Spring 过滤器在初始化函数后丢失设置变量

java - 创建WAR打包的maven项目时, 'Deployed Resources'文件夹是什么?

java - hadoop 和 yarn 的 Maven 依赖项

git - 发布托管在单个 Git 存储库中的多模块 Maven 项目

python - Bokeh 'utf8' 编解码器无法解码字节 0xe9 : unexpected end of data

php - 使用 gmail 中的 imap_fetch 时编码的俄语