Skip to content

Instantly share code, notes, and snippets.

@vaughandroid
Last active March 20, 2020 08:29
Show Gist options
  • Save vaughandroid/99ce457e62f74ad9be2f794f014e3c8d to your computer and use it in GitHub Desktop.
Save vaughandroid/99ce457e62f74ad9be2f794f014e3c8d to your computer and use it in GitHub Desktop.
JUnit TestRule for using https://github.com/dlew/joda-time-android in JVM unit tests
package me.vaughandroid.testutils.rules;
import org.joda.time.DateTimeZone;
import org.joda.time.tz.Provider;
import org.joda.time.tz.UTCProvider;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* A {@link TestRule} which prevents errors when using the
* <a href="https://github.com/dlew/joda-time-android">joda-time-android</a> lib in JVM unit tests.
* Without this rule, tests will log a caught exception:
* {@code java.io.IOException: Resource not found: "org/joda/time/tz/data/ZoneInfoMap"}
*
* @author [email protected]
*/
public class JodaAndroidFixRule implements TestRule {
private final Provider provider;
public JodaAndroidFixRule() {
this(new UTCProvider());
}
public JodaAndroidFixRule(Provider provider) {
this.provider = provider;
}
@Override public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override public void evaluate() throws Throwable {
DateTimeZone.setProvider(provider);
base.evaluate();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment