Last active
March 9, 2022 17:16
-
-
Save digulla/5884162 to your computer and use it in GitHub Desktop.
JUnit 4 Rule to run individual tests with a different default locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Locale; | |
import org.junit.rules.TestWatcher; | |
import org.junit.runner.Description; | |
public class DefaultLocaleRule extends TestWatcher { | |
private Locale originalDefault; | |
private Locale currentDefault; | |
public DefaultLocaleRule() { | |
this( null ); | |
} | |
public DefaultLocaleRule( Locale defaultForTests ) { | |
currentDefault = defaultForTests; | |
} | |
@Override | |
protected void starting( Description description ) { | |
originalDefault = Locale.getDefault(); | |
if( null != currentDefault ) { | |
Locale.setDefault( currentDefault ); | |
} | |
} | |
@Override | |
protected void finished( Description description ) { | |
Locale.setDefault( originalDefault ); | |
} | |
public void setDefault( Locale locale ) { | |
if( null == locale ) { | |
locale = originalDefault; | |
} | |
Locale.setDefault( locale ); | |
} | |
public static DefaultLocaleRule en() { | |
return new DefaultLocaleRule( Locale.ENGLISH ); | |
} | |
public static DefaultLocaleRule de() { | |
return new DefaultLocaleRule( Locale.GERMAN ); | |
} | |
public static DefaultLocaleRule fr() { | |
return new DefaultLocaleRule( Locale.FRENCH ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To use the rule, add this to your JUnit test class: | |
// make all tests run with the English locale | |
@Rule | |
public DefaultLocaleRule defaultLocaleRule = DefaultLocaleRule.en(); | |
If a test has special demands, you can switch: | |
@Test | |
public void testDE() { | |
defaultLocaleRule.setDefault( Locale.GERMAN ); | |
... | |
} |
I found the reason. It seems like @BeforeClass block are run first, then @rule block is run. So the solute I found is to replace the @rule annotation with @ClassRule that is run before @BeforeClass block and the rule variable should be static:
@ClassRule
public static DefaultLocaleRule defaultLocaleRule = DefaultLocaleRule.fr();
@BeforeClass
public static void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It fails when using
before_class
and javax.validation.Validator as follows:Here is the output:
It seems like Validator does not take it in account.
By te way, if I remove the commented code that forces th FR locale in
before_class
block, and remove all the call to DefaultRule class, it works as need. Unfortunately, it is not so DRY and the FR locale has to beard coded :(.Any idea about that?
Thank you.