A test rule for setting device locale

When you run your Android tests (like espresso tests), you may want to be able to force the locale of your device to some specific value at runtime (during test execution). This could be really helpful if you want to test some features of your app against multiple locales. You can do this by using Junit4 rules. The rule implementation looks like this: public class ForceLocaleRule implements TestRule { private final Locale mTestLocale; private Locale mDeviceLocale; public ForceLocaleRule(Locale testLocale) { mTestLocale = testLocale; } @Override public Statement apply(Statement base, Description description) { return new Statement() { public void evaluate() throws Throwable { try { if (mTestLocale !...

May 9, 2016 ยท 2 min