No possibility to set file.encoding for junit tests in Gradle 2.13 and odler


(Manfred Schenk) #1

After switching from eclipse projects to gradle projects I’m facing a problem concerning the file.encoding settings:

The junit tests of my projects run well under eclipse (in the original eclipse projects as well as in the eclipse projects generated by the gradle eclipse plugin) but fail under gradle. When I add some logging statement to those tests I can see that in the eclipse cases the Property file.encoding is set to utf-8 while in the gradle case it reports windows-1252.
I managed to set the encoding for the compiler by adding options.encoding = 'UTF-8' to compileJava task but I found no similar solution for setting the encoding for the Testrunner.

The only workaround for now is setting the System-Environmentvariable GRADLE_OPTS=-Dfile.encoding=utf-8 , but I consider this a very ugly workaround since it might harm other builds on the Continuous integration environment.

There is also a similar question on Stackoverflow --> http://stackoverflow.com/questions/36263600/encoding-in-junit-gradle-vs-eclipse


(PetaCZ) #2

Hello Manfred,

I am using this in the main build.gradle file:

allprojects {
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
        
    tasks.withType(Test) {
       systemProperty "file.encoding", "UTF-8"
    }
}

(Manfred Schenk) #3

I just tested your suggestion: but the test still fails. So the setting of GRADLE_OPTS has other effects besides the results of setting options.encoding.


(PetaCZ) #4
  1. You can use Gradle Wrapper and specify GRADLE_OPTS option in gradlew and gradlew.bat files.
    https://docs.gradle.org/current/userguide/gradle_wrapper.html

  2. You don’t have to use this GRADLE_OPTS for all builds on CI, use it only for affected builds.


(Manfred Schenk) #5

After some more research I’ve finally found the where the problem was:

The real problem was not the wrong encoding of the test runtime - it was the wrong encoding of the compileTestJava task: I had anticipated that setting utf-8 for compileJava would also set utf-8 for the compileTestJava used for compilation of the junit test cases. Since a configuration for compileJava was already present in my build.gradle I had only copied the “withType(test)” part of your example.

So - thanks for your help


(PetaCZ) #6

I’m glad that it helped.