Search code examples
javaspringmavenutf-8integration-testing

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


Since I updated my Windows 10 and installed new OpenJDK 11.0.4 my Spring integration tests are not running anymore in the Maven context. Starting it within Eclipse is fine and still working but running it with mvn clean install it is not working aynmore with the error:

   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)

I have set UTF-8 encoding everywhere I was able to set.

  1. In the application-test.properties
file.encoding=UTF-8
spring.mandatoryFileEncoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
  1. in the maven properties and plugin configurations:
    <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. In the integration tests properties:
    @RunWith(SpringRunner.class)
    @SpringBootTest(properties = "file.encoding=UTF-8", classes = SEBServer.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  1. While starting maven as a VM argument

mvn clean install -Dfile.encoding=UTF-8

can anyone tell me where else I can set the encoding? there seems to be as many possible places as stars in the sky but nowhere the right one.

Thanks!


Solution

  • It seems that the encoding Cp1252 is used by default in some legacy Windows Components (see: https://en.wikipedia.org/wiki/Windows-1252#:~:text=Windows%2D1252%20or%20CP%2D1252,character%20encoding%20in%20the%20world)
    As the OP mentions that the problem (integration/unit tests not running any more) go's away if the parameter -Dfile.encoding=UTF-8 is added to the mvn invocation, it seems that we need a way to hard code this in the pom.xml.
    This may be done by including a systemPropertyVariables child in the configuration element of the unit test plugin maven-surefire-plugin. A similar systemPropertyVariables child exists for the integration test plugin maven-failsafe-plugin.
    References: https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html
    and
    https://maven.apache.org/surefire/maven-failsafe-plugin/examples/system-properties.html