Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit-tests and joda-time-android

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?

like image 629
Alexandr Avatar asked May 12 '16 15:05

Alexandr


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.

like image 116
Alexandr Avatar answered Oct 22 '22 06:10

Alexandr


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.

like image 37
npace Avatar answered Oct 22 '22 04:10

npace