I created a library which can be used in an Android environment. At the moment it contains Plain Old Groovy Objects, there are no Android dependencies. Tests for those POGOs run without problems.
To extend the library's features I create classes which inherit from Android classes or rely on them, I want to test those too. Of course tests should be part of the library and should not be implemented as a part of an Android application project which includes the library.
I know that an application can be tested with the help of Robolectric and it works pretty well but I see no possibility of using it without a whole Android application behind it.
Is there a way to test a library with Android dependencies and Robolectric?
I am using Android Studio 1.2.
setupActivity() is deprecated in Android unit test. Save this question.
Robolectric is a framework that brings fast and reliable unit tests to Android. Tests run inside the JVM on your workstation in seconds. With Robolectric you can write tests like this: @RunWith(RobolectricTestRunner.
So the main benefit about Robolectric is that it is a lot faster than Espresso or Instrumented tests in general. The downside is that it fakes an Android environment which you should be aware of. To validate real world problems, better use a classic Android Framework approach.
I think that Robolectric is a good choice for you. For example if you wrote a class that inherits from the DialogFragment you can see if it behaves correctly:
@RunWith(RobolectricTestRunner.class)
public class ProgressDialogFragmentTest {
private static final String MESSAGE_KEY = "KEY_PROGRESS_MESSAGE";
private static final String TEST_MESSAGE = "test message";
@Test
public void argumentsContainExpectedKey() {
//given
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(TEST_MESSAGE);
//when
boolean containsExpectedKey = fragment.getArguments().containsKey(MESSAGE_KEY);
//then
assertTrue(containsExpectedKey);
}
@Test
public void argumentsContainsValidMessage() {
//given
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(TEST_MESSAGE);
//when
String actualMessage = fragment.getArguments().getString(MESSAGE_KEY);
//then
assertEquals(TEST_MESSAGE, actualMessage);
}
@Test
public void createdDialogIsProgressDialog() {
//given
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(TEST_MESSAGE);
FragmentTestUtil.startFragment(fragment);
//when
Dialog dialog = fragment.getDialog();
//then
assertTrue(dialog instanceof ProgressDialog);
}
@Test
public void dialogIsHiddenBeforeRunShowMethod() {
//given
ProgressDialogFragment fragment = ProgressDialogFragment.newInstance(TEST_MESSAGE);
FragmentTestUtil.startFragment(fragment);
//when
ProgressDialog dialog = (ProgressDialog) fragment.onCreateDialog(fragment.getArguments());
boolean showing = dialog.isShowing();
//then
assertFalse(showing);
}
}
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