5

I have troubles with character encoding in my JPA test class.

In my h2 in memory database I have this insert query :

INSERT INTO MYTABLE (ID,FIELD1,FIELD2) VALUES (100,'ABC','Réclamation');

(please notice the "é" character in "Réclamation")

In my JUnit Test, I try to assert that the value of FIELD2 column is equal to "Réclamation" (which is the case as you can see)

But it fails with the following error :

org.junit.ComparisonFailure: expected:R[é]clamation but was: R[�]clamation

I wonder if there is a way to specify character encoding in persistence.xml file (maybe ? or somewhere else)

Here is my persistence.xml test file :

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="myTestPU">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.myproject.myclass1</class>
        <class>com.myproject.myclass2</class>
        <properties>
            <property
                name="javax.persistence.schema-generation.database.action"
                value="none" />
            <property
                name="javax.persistence.sharedCache.mode"
                value="ENABLE_SELECTIVE" />
            <property
                name="javax.persistence.jdbc.url"
                value="jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:ddl/schema.sql'\;RUNSCRIPT FROM 'classpath:ddl/data.sql'" />
            <property
                name="javax.persistence.jdbc.driver"
                value="org.h2.Driver" />
            <property
                name="hibernate.dialect"
                value="org.hibernate.dialect.H2Dialect" />
            <property
                name="hibernate.show_sql"
                value="true" />
            <property
                name="hibernate.format_sql"
                value="true" />
        </properties>
    </persistence-unit>
</persistence>

I've already tried these solutions :

  • adding the following properties in persistence.xml test file :

    <property
        name="hibernate.connection.characterEncoding"
        value="utf8" />
    <property
        name="hibernate.connection.useUnicode"
        value="true" />
    <property
        name="hibernate.connection.charSet"
        value="UTF-8" />
    
  • adding ?useUnicode=yes&amp;characterEncoding=UTF-8 in my URL property

None of them worked for me...

NB : I don't use spring framework in my application

Sinda MOKADDEM
  • 796
  • 2
  • 12
  • 35

3 Answers3

2

If you are using Spring Boot tests with @SpringBootTest annotation, then the H2 database should be using whatever encoding is passed inside your JVM arguments (if none is passed, then it will use the environment default). In Spring Boot environment you can use following inside your POM to change the encoding to UTF-8:

<properties>
    <spring-boot.run.jvmArguments>-Dfile.encoding=UTF-8</spring-boot.run.jvmArguments>
</properties>

Also ensure that the files you are reading into the database (the ones containing your SQL inserts) are in the same encoding as your environment.

Jukka Hämäläinen
  • 647
  • 1
  • 6
  • 17
1

I had my insert queries in an import.sql file and those values were encoded incorrectly. Adding the property <property name="hibernate.hbm2ddl.charset_name" value="UTF-8" /> fixed it for me.

If you have an older version of hibernate (pre 5.2.3 apparently), maybe try the other solutions in this thread: Hibernate/JPA import.sql utf8 characters corrupted

1

adding to Jukka Hämäläinen's answer, you can pass this argument to surefire plugin (based on this answer) like this:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <argLine>-Dfile.encoding=UTF-8</argLine>
    </configuration>
 </plugin>
hello_earth
  • 1,442
  • 1
  • 25
  • 39