I used joda-time-android
library's classes in my Presenter
class and it must not have any Android dependencies. So I can't test it properly. I know that I can use Robolectric
for this, but I want to keep out of this tool and my Presenter
clean. Should I abandon JodaTime? Any other solution?

- 3,859
- 5
- 32
- 56
2 Answers
For Unit-tests
we can use default java implementation of JodaTime
library because it has the same package and classes as joda-time-android
.
// for android
compile 'net.danlew:android.joda:2.9.3'
// joda-time for tests
testCompile 'joda-time:joda-time:2.9.3'
Works like a charm.
For other cases with other libraries see @npace's answer.

- 3,859
- 5
- 32
- 56
-
Doesn't work for me for some reason, same error 'Need to call JodaTimeAndroid.init() before using joda-time-android'... What is the "default java implementation of JodaTime library", where to get it? – Anton Malyshev May 01 '20 at 15:47
Well, joda-time-android
does depend on the Android runtime in order to work. Using Robolectric
for this seems like an okay use case, since you won't be interacting with Fragment
, Activity
and other UI classes from the Android SDK.
Since setting up Robolectric
is kind of a pain, there's another option - you could use the Adapter pattern to create a common interface for the regular Java Joda time library and the Android version.
If that's possible and practical (depending on how much of the Joda time API you want to use), you can back your Adapter
with joda-time-android
when running the app, and back it with joda-time
when running the unit tests. I imagine that you can make it use the Android concrete implementation from your Application
's onCreate()
method and the Java concrete implementation from your unit test's @Before
, for example.

- 4,218
- 1
- 25
- 35
-
I used Adapter pattern for `Log` class. But in case of JodaTime there are too many interfaces to write. Your answer has pushed me to more elegant solution. – Alexandr May 12 '16 at 16:33