I'm trying to test a data access library which contains Spring Data JPA entites and repositories.
My SpringBootTests create a database and populates it automatically with schema-test.sql and data-test.sql
In my application.properties:
spring.datasource.initialization-mode=always
spring.datasource.platform=test
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.hibernate.ddl-auto=none
In my data-test.sql, I have some latin characters. Something like:
INSERT INTO TABLE
(ID, NAME)
VALUES
(1, 'Donnée');
My data-test.sql file is UTF-8 encoded. I doubled checked with Vim :se fenc
and in Eclipse:
When I launch my JUnit test from Eclipse, everything works, my tests pass. But when I launch a maven package goal from Eclipse, my tests fail with something like:
Expecting:
<Optional[TableEntity [id=1, name=Donn??e]]>
to contain:
<TableEntity [id=1, name=Donnée]>
but did not.
It seems that maven does not interpret my data-test.sql file as an UTF-8 encoded file, but as a CP-1252 encoded file.
I added the following in my pom.xml:
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
and also:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<!-- Latin1 is still the default for .properties files -->
<propertiesEncoding>ISO-8859-1</propertiesEncoding>
</configuration>
</plugin>
In the Eclipse "Run configuration", I added the following VM arguments :
-Dfile.encoding=UTF-8
and I also force the encoding in the "Common" tab:
In the console, I find:
[DEBUG] Copying file application.properties
[DEBUG] file application.properties has a filtered file extension
[DEBUG] Using 'ISO-8859-1' encoding to copy filtered resource 'application.properties'.
[DEBUG] copy (...)srcest
esourcesapplication.properties to (...)argetest-classesapplication.properties
[DEBUG] Copying file data-test.sql
[DEBUG] file data-test.sql has a filtered file extension
[DEBUG] Using 'UTF-8' encoding to copy filtered resource 'data-test.sql'.
[DEBUG] copy (...)srcest
esourcesdata-test.sql to (...)argetest-classesdata-test.sql
[DEBUG] Copying file schema-test.sql
[DEBUG] file schema-test.sql has a filtered file extension
[DEBUG] Using 'UTF-8' encoding to copy filtered resource 'schema-test.sql'.
[DEBUG] copy (...)srcest
esourcesschema-test.sql to (...)argetest-classesschema-test.sql
Is there a hidden option somewhere forcing Maven, or my SpringBootTest class to interpret the SQL file as an old CP-1252 or ISO-8859-1 encoding?
A full example is available on GitHub.
question from:
https://stackoverflow.com/questions/65885856/encoding-issue-with-data-sql-in-spring-boot-with-maven