Georgian
Georgian

Reputation: 8960

Encoding in JUnit: Gradle vs Eclipse

I have a project with some JUnit model tests, in Eclipse.

One of these tests asserts some special characters. It passes in Eclipse (Run As -> JUnit Test), but fails when ran with Gradle (clean test).

The failure: org.junit.ComparisonFailure: expected:<[ü]> but was:<[�]>, in the Gradle report.

I've added tasks.withType(JavaCompile) {options.encoding = 'UTF-8} to the build file, which fixed the compile-time encoding issues. But I still get the run-time error (see the failure above).

Adding compileJava.options.encoding = 'UTF-8' does not help.

The test runs on MacOS, and the project encoding is inherited from the OS (Windows + UTF-8, in my case).

Upvotes: 4

Views: 3537

Answers (2)

Duncan Harris
Duncan Harris

Reputation: 657

You can use this to cover all Java compilation of both test and production code:

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

Upvotes: 0

Thomas Auinger
Thomas Auinger

Reputation: 2095

You need to set the encoding for the task, which compiles the test classes, too:

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

Upvotes: 8

Related Questions