Drew Noakes
Drew Noakes

Reputation: 311255

jUnit testing Double.toString in multiple cultures

I have an open source library which has plenty of unit tests that compare string forms of numbers.

These tests pass fine in en-GB, en-US and other cultures where numbers are generally written in the form 1,234.00.

However in cultures such as Germany and France, these values are formatted differently, and the tests fail.

How can the jUnit tests be forced to run as en-GB?

EDIT this kind of thing is available in NUnit.

Upvotes: 3

Views: 1182

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328754

there are two gists with JUnit 4 rules to modify the default locale for a couple of tests:

LocaleRule, a very simple implementation.

DefaultLocaleRule has some static helper methods and allows to switch the default locale for an individual test.

Upvotes: 0

ptyx
ptyx

Reputation: 4164

How do you launch jUnit?

Passing the appropriate language property will depend more of your environment than of jUnit itself.

Alternatively (and I think it's a better solution), you could compare values rather than strings:

assertEquals(12.3, Double.valueOf(aDoubleString));
assertEquals(Double.toString(12.3), aDoubleString);

rather than

assertEquals("12.3", aDoubleString)

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692023

I'm not sure it's standard for all JVMs, but using Oracle's JVM on Windows, you can use the user.language and user.country System properties to set the locale when starting the JVM:

java -Duser.language=en -Duser.country=GB ...

You can also, of course, set the default locale in Java, using

Locale.setDefault(new Locale("en", "GB"));

Note that Double.toString is locale-independent, though.

Upvotes: 4

Related Questions