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

UTF-8 encoding not set for Spring integration tests running with Maven on Windows

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

   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. 申请中-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

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

谢谢!

似乎在某些遗留 Windows 组件中默认使用编码 Cp1252(参见:https://en.wikipedia.org/wiki/Windows-1252#:~:text=Windows%2D1252%20or%20CP%2D1252,character%20encoding%20in%20the%20world
由于 OP 提到如果将参数 -Dfile.encoding=UTF-8 添加到 mvn 调用中,问题(integration/unit 不再测试 运行 就会消失,看来我们需要一个在 pom.xml.
中硬编码的方法 这可以通过在单元测试插件 maven-surefire-pluginconfiguration 元素中包含一个 systemPropertyVariables 子元素来完成。集成测试插件 maven-failsafe-plugin.
存在类似的 systemPropertyVariables 子项 参考文献:https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

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