gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:03:59 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com