在 Windows 上使用 Maven 运行的 Spring 集成测试未设置 UTF-8 编码

问题描述 投票:0回答:3

自从我更新了 Windows 10 并安装了新的 OpenJDK 11.0.4 后,我的 Spring 集成测试就不再在 Maven 上下文中运行。在 Eclipse 中启动它很好,并且仍然可以工作,但是使用 mvn clean install 运行它,它不起作用 aynmore 并出现错误:

   18:01:01.340 ERROR [main]:[org.springframework.boot.context.FileEncodingApplicationListener] System property 'file.encoding' is currently 'Cp1252'. It should be 'UTF-8' (as defined in 'spring.mandatoryFileEncoding').
18:01:01.341 ERROR [main]:[org.springframework.boot.context.FileEncodingApplicationListener] Environment variable LANG is 'null'. You could use a locale setting that matches encoding='UTF-8'.
18:01:01.341 ERROR [main]:[org.springframework.boot.context.FileEncodingApplicationListener] Environment variable LC_ALL is 'null'. You could use a locale setting that matches encoding='UTF-8'.
18:01:01.344 ERROR [main]:[org.springframework.boot.SpringApplication] Application run failed
java.lang.IllegalStateException: The Java Virtual Machine has not been configured to use the desired default character encoding (UTF-8).
    at org.springframework.boot.context.FileEncodingApplicationListener.onApplicationEvent(FileEncodingApplicationListener.java:76)
    at org.springframework.boot.context.FileEncodingApplicationListener.onApplicationEvent(FileEncodingApplicationListener.java:47)

我已经在所有能够设置的地方设置了 UTF-8 编码。

  1. 在application-test.properties中
file.encoding=UTF-8
spring.mandatoryFileEncoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
  1. 在maven属性和插件配置中:
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>



  <profiles>
    <profile>
      <id>Java 11</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <java.version>11</java.version>
      </properties>
      <build>
        <finalName>${project.artifactId}-${build-version}</finalName>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <release>${java.version}</release>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
        </plugins>
      </build>
      <dependencies>
        <dependency>
          <groupId>org.glassfish.jaxb</groupId>
          <artifactId>jaxb-runtime</artifactId>
          <version>2.4.0-b180830.0438</version><!--$NO-MVN-MAN-VER$ --> <!-- TODO move to stable version when available -->
        </dependency>
      </dependencies>
    </profile>
  1. 在集成测试属性中:
    @RunWith(SpringRunner.class)
    @SpringBootTest(properties = "file.encoding=UTF-8", classes = SEBServer.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  1. 将 Maven 作为 VM 参数启动时

mvn clean install -Dfile.encoding=UTF-8

谁能告诉我还可以在哪里设置编码?天空中的星星似乎有很多可能的地方,但没有一个是正确的。

谢谢!

java spring maven utf-8 integration-testing
3个回答
2
投票

似乎在某些旧版 Windows 组件中默认使用编码

Cp1252
(请参阅:https://en.wikipedia.org/wiki/Windows-1252#:~:text=Windows%2D1252%20or%20CP %2D1252,字符%20编码%20in%20the%20world)
正如OP提到的,如果将参数
-Dfile.encoding=UTF-8
添加到
mvn
调用中,问题(集成/单元测试不再运行)就会消失,看来我们需要一种方法在
pom.xml
中对此进行硬编码.
这可以通过在单元测试插件
systemPropertyVariables
configuration
元素中包含
maven-surefire-plugin
子元素来完成。集成测试插件
systemPropertyVariables
也存在类似的
maven-failsafe-plugin
子项。
参考文献:https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

https://maven.apache.org/surefire/maven-failsafe-plugin/examples/system-properties.html


1
投票

这对我有用:

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

0
投票

我有同样的问题,我通过添加这个解决了它

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
    </configuration>
</plugin>

© www.soinside.com 2019 - 2024. All rights reserved.