Testing android jsr310 backport

code snippet

When dealing with dates on android I use jsr310 Android backport by Jake Wharton . This library loads timezone data on app startup using AndroidThreeTen.init(this). Unfortunately this uses context (this = Application) which is not aviable in unit tests.

So just calling some functions that use dates will end in crash: org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered

How to fix that? First thing to do is create resources folder in test path (f.e. project/src/test/resources). Than copy time zone database into that folder. After that I’m using following snippet (in Kotlin) to load database:

fun Any.loadTimeZone() {
    if (ZoneRulesProvider.getAvailableZoneIds().isEmpty()) {
        val stream = this.javaClass.classLoader.getResourceAsStream("TZDB.dat")
        stream.use(::TzdbZoneRulesProvider).apply {

just calling this in any test class constructor:

class SomeTestClass {

    init {

    fun shouldTest() {
        //your test here

Happy testing!