Last active
March 20, 2020 08:29
-
-
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
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
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