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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With