Carlos
Carlos

Reputation: 2513

Setting encoding for jUnit task in Ant

I have a test case (it is really an integration test) which logs in with a username containing Scandinavian letters. The problem is that when I run the Ant task from command line, the authentication fails because the encoding is not correct (it should be UTF-8). The test runs just fine when I run it from Eclipse, but not from command line. So far I've tried to tell the correct encoding to Ant both in the Ant target:

<target name="run_tests">
    <junit fork="no" haltonfailure="no">
        <jvmarg value="-Dfile.encoding=UTF-8"/>
        <formatter type="xml" usefile="true" />
        <classpath refid="test.classpath" />
        <test name="com.company.integration.AllIntegrationTests" />
    </junit>
</target>

and from the command line:

ant -D"file.encoding=UTF-8" run_tests

Neither of these work. Whatever I do, the tests still fail and the test report says:

<property name="file.encoding" value="cp1252" />

Like I said, if I run it from Eclipse, everything works beautifully. I also noticed that if I modify the run configuration in Eclipse for the test by changing the encoding to ISO-8859-1, the test fails like expected. So obviously it is possible to change the encoding, but how do you do it?

Upvotes: 4

Views: 3495

Answers (1)

ewan.chalmers
ewan.chalmers

Reputation: 16235

You will need to use fork=yes to execute JUnit in a separate JVM.

As it is, file.encoding is being inherited from Ant's JVM and is not overridden by your jvmarg.

See the documentation for jvmarg in the JUnit Task manual page:

If fork is enabled, additional parameters may be passed to the new VM via nested elements.

Upvotes: 6

Related Questions