Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing android apps

Unit Testing android apps

Avatar for Andrew Drobyazko

Andrew Drobyazko

March 18, 2016
Tweet

More Decks by Andrew Drobyazko

Other Decks in Programming

Transcript

  1. What is Unit Testing? Essentially, a unit test is a

    method that instantiates a small portion of our application and verifies its behavior independently from other parts.
  2. A typical unit test contains 3 phases: 1 - It

    initializes a small piece of an application it wants to test 2 - It applies some stimulus to the system under test (usually by calling a method on it) 3 - And finally it observes the resulting behavior.
  3. Local Unit tests • Running on JVM on the local

    machine • Src/test/java/MyTest.java • JUnit 4 is used • No access to Android API • No access to the real device • Lightning fast • Easy mocking dependencies • No need of dependency injections
  4. Instrumented Tests • Running on the real Android device, or

    emulator. • src/androidTest/java/MyAndroidTest.java • Access to Instrumentation information. • Can be used for Unit testing, User Interface testing, Integration testing. • Can be automated. • Android testing support library is used for unit testing (AndroidJUnitRunner). • Espresso is used for UI testing. • Access to Context, Database, SharedPreferences, Network etc… • Dependencies can be injected.
  5. Local Unit test example import org.junit.Test; import java.util.regex.Pattern; import static

    org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class EmailValidatorTest { @Test public void emailValidator_CorrectEmailSimple_ReturnsTrue() { assertThat(EmailValidator.isValidEmail("[email protected]"), is(true)); } ... }
  6. Local Unit test + Mockito import static org.hamcrest.MatcherAssert.assertThat; import static

    org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import android.content.SharedPreferences; @RunWith(MockitoJUnitRunner.class) public class UnitTestSample { private static final String FAKE_STRING = "HELLO WORLD"; @Mock Context mMockContext; @Test public void readStringFromContext_LocalizedString() { // Given a mocked Context injected into the object under test... when(mMockContext.getString(R.string.hello_word)) .thenReturn(FAKE_STRING); ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext); // ...when the string is returned from the object under test... String result = myObjectUnderTest.getHelloWorldString(); // ...then the result should be the expected one. assertThat(result, is(FAKE_STRING)); } }
  7. Instrumented Unit test example import android.os.Parcel; import android.support.test.runner.AndroidJUnit4; import android.util.Pair;

    import org.junit.Test; import org.junit.runner.RunWith; import java.util.List; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(AndroidJUnit4.class) @SmallTest public class LogHistoryAndroidUnitTest { public static final String TEST_STRING = "This is a string"; public static final long TEST_LONG = 12345678L; private LogHistory mLogHistory; @Before public void createLogHistory() { mLogHistory = new LogHistory(); } @Test public void logHistory_ParcelableWriteRead() { // Set up the Parcelable object to send and receive. mLogHistory.addEntry(TEST_STRING, TEST_LONG); // Write the data. Parcel parcel = Parcel.obtain(); mLogHistory.writeToParcel(parcel, mLogHistory.describeContents()); // After you're done with writing, you need to reset the parcel for reading. parcel.setDataPosition(0); // Read the data. LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel); List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData(); // Verify that the received data is correct. assertThat(createdFromParcelData.size(), is(1)); assertThat(createdFromParcelData.get(0).first, is(TEST_STRING)); assertThat(createdFromParcelData.get(0).second, is(TEST_LONG)); }
  8. MVP Do we need to unit-test Model, View, or Presenter?


    
 
 Model only. View Presenter Model
  9. MVP Presenter Interactor Interactor accepts RequestModel Interactor returns ResultModel We

    can test an Interactor using RequestModel and ResultModel.