【发布时间】:2021-01-25 13:37:11
【问题描述】:
我正在尝试测试一个包含 Spring Data JPA 实体和存储库的数据访问库。
我的 SpringBootTests 创建一个数据库并使用 schema-test.sql 和 data-test.sql 自动填充它
在我的 application.properties 中:
spring.datasource.initialization-mode=always
spring.datasource.platform=test
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.hibernate.ddl-auto=none
在我的 data-test.sql 中,我有一些拉丁字符。比如:
INSERT INTO TABLE
(ID, NAME)
VALUES
(1, 'Donnée');
我的 data-test.sql 文件是 UTF-8 编码的。我用 Vim :se fenc 和 Eclipse 加倍检查:
当我从 Eclipse 启动 JUnit 测试时,一切正常,我的测试通过了。但是当我从 Eclipse 启动一个 maven 包目标时,我的测试失败了,如下所示:
Expecting:
<Optional[TableEntity [id=1, name=Donnée]]>
to contain:
<TableEntity [id=1, name=Donnée]>
but did not.
maven 似乎没有将我的 data-test.sql 文件解释为 UTF-8 编码文件,而是 CP-1252 编码文件。
我在 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>
还有:
<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>
在 Eclipse “运行配置”中,我添加了以下 VM 参数:
-Dfile.encoding=UTF-8
在控制台中,我发现:
[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 (...)\src\test\resources\application.properties to (...)\target\test-classes\application.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 (...)\src\test\resources\data-test.sql to (...)\target\test-classes\data-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 (...)\src\test\resources\schema-test.sql to (...)\target\test-classes\schema-test.sql
是否存在强制 Maven 或我的 SpringBootTest 类将 SQL 文件解释为旧的 CP-1252 或 ISO-8859-1 编码的隐藏选项?
一个完整的例子是可用的on GitHub。
【问题讨论】:
标签: eclipse spring-boot maven character-encoding