首页 > 解决方案 > datajptest + springframework 测试Sql编码特殊字符在Windows上失败

问题描述

在 Windows (+ Maven) 上,我éMétro单词有疑问。我的 sql 文件以 UTF-8 编码。我在 Unix 服务器上确实有这个问题。

我的 Maven 错误:

expected:<M[é]tro Ligne 6, station...> but was:<M[é]tro Ligne 6, station...>

我的 JUnit 代码:

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
public class MyRepositoryTest {

    @Autowired
    private MyRepository myRepository;

    @Test
    @Sql("/data/myRepositoryTest.sql")
    public void testFindById() {
        Optional<My> my= myRepository.findById(99999);
        assertTrue(my.isPresent());
        assertEquals("Métro Ligne 6, station Bel-Air", my.get().getItinerary());
    }

}

我的 sql 文件:

INSERT INTO MY(ID, NAME, CODE, ITINERARY)
  VALUES (99999, 'foo', 'abc', 'Métro Ligne 6, station Bel-Air');

标签: javamavenjpatestingjunit

解决方案


The default encoding is taken from the operation system, hence the problem.

Set the SQL file encoding like this:

@Sql(value = "/data/myRepositoryTest.sql", config = @SqlConfig(encoding = "utf-8"))

推荐阅读