$30 off During Our Annual Pro Sale. View Details »

Unit Testing in a Nutshell - DroidKaigi 2018

Unit Testing in a Nutshell - DroidKaigi 2018

Unit Testing in a Nutshell - DroidKaigi 2018
This is a reference material for this session
https://droidkaigi.jp/2018/timetable?session=16942
Hands-On
https://github.com/srym/DroidKaigi2018UnitTestHandOn

Fumihiko Shiroyama

February 02, 2018
Tweet

More Decks by Fumihiko Shiroyama

Other Decks in Technology

Transcript

  1. Unit Testing in a Nutshell
    Fumihiko Shiroyama
    DroidKaigi 2018

    View Slide

  2. Who is this guy?
    • Senior Android Developer @nikkei
    • Executive Beer Drinker
    • Chief Baby Sitter at home
    • Unit Test Enthusiast

    View Slide

  3. Agenda today
    • Why you should care about Unit Testing
    • JUnit4 and basic assertions
    • Mock and spy with Mockito
    • Robolectric for framework's code
    • Unit Testing SQLite
    • Unit Testing asynchronous code
    • Best practices and tips

    View Slide

  4. Why you should care about Unit Testing?

    View Slide

  5. What if you DON'T?

    View Slide

  6. public class LoginActivity extends AppCompatActivity implements LoaderCallbacks {
    private static final int REQUEST_READ_CONTACTS = 0;
    private static final String[] DUMMY_CREDENTIALS = new String[]{
    "[email protected]:hello", "[email protected]:world"
    };
    private UserLoginTask mAuthTask = null;
    private AutoCompleteTextView mEmailView;
    private EditText mPasswordView;
    private View mProgressView;
    private View mLoginFormView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    populateAutoComplete();
    mPasswordView = (EditText) findViewById(R.id.password);
    mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
    if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
    attemptLogin();
    return true;
    }
    return false;
    }
    });
    Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
    mEmailSignInButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
    attemptLogin();
    }
    });
    mLoginFormView = findViewById(R.id.login_form);
    mProgressView = findViewById(R.id.login_progress);
    }
    private void populateAutoComplete() {
    if (!mayRequestContacts()) {
    return;
    }
    getLoaderManager().initLoader(0, null, this);
    }
    private boolean mayRequestContacts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    return true;
    }
    if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
    return true;
    }
    if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
    Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
    .setAction(android.R.string.ok, new View.OnClickListener() {
    @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void onClick(View v) {
    requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    });
    } else {
    requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    return false;
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_CONTACTS) {
    if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    populateAutoComplete();
    }
    }
    }
    private void attemptLogin() {
    if (mAuthTask != null) {
    return;
    }
    mEmailView.setError(null);
    mPasswordView.setError(null);
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();
    boolean cancel = false;
    View focusView = null;
    if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
    mPasswordView.setError(getString(R.string.error_invalid_password));
    focusView = mPasswordView;
    cancel = true;
    }
    if (TextUtils.isEmpty(email)) {
    mEmailView.setError(getString(R.string.error_field_required));
    focusView = mEmailView;
    cancel = true;
    } else if (!isEmailValid(email)) {
    mEmailView.setError(getString(R.string.error_invalid_email));
    focusView = mEmailView;
    cancel = true;
    }
    if (cancel) {
    focusView.requestFocus();
    } else {
    showProgress(true);
    mAuthTask = new UserLoginTask(email, password);
    mAuthTask.execute((Void) null);
    }
    }

    View Slide

  7. public class LoginActivity extends AppCompatActivity implements LoaderCallbacks {
    private static final int REQUEST_READ_CONTACTS = 0;
    private static final String[] DUMMY_CREDENTIALS = new String[]{
    "[email protected]:hello", "[email protected]:world"
    };
    private UserLoginTask mAuthTask = null;
    private AutoCompleteTextView mEmailView;
    private EditText mPasswordView;
    private View mProgressView;
    private View mLoginFormView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    populateAutoComplete();
    mPasswordView = (EditText) findViewById(R.id.password);
    mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
    if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
    attemptLogin();
    return true;
    }
    return false;
    }
    });
    Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
    mEmailSignInButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
    attemptLogin();
    }
    });
    mLoginFormView = findViewById(R.id.login_form);
    mProgressView = findViewById(R.id.login_progress);
    }
    private void populateAutoComplete() {
    if (!mayRequestContacts()) {
    return;
    }
    getLoaderManager().initLoader(0, null, this);
    }
    private boolean mayRequestContacts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    return true;
    }
    if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
    return true;
    }
    if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
    Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
    .setAction(android.R.string.ok, new View.OnClickListener() {
    @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void onClick(View v) {
    requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    });
    } else {
    requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    return false;
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_CONTACTS) {
    if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    populateAutoComplete();
    }
    }
    }
    private void attemptLogin() {
    if (mAuthTask != null) {
    return;
    }
    mEmailView.setError(null);
    mPasswordView.setError(null);
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();
    boolean cancel = false;
    View focusView = null;
    if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
    mPasswordView.setError(getString(R.string.error_invalid_password));
    focusView = mPasswordView;
    cancel = true;
    }
    if (TextUtils.isEmpty(email)) {
    mEmailView.setError(getString(R.string.error_field_required));
    focusView = mEmailView;
    cancel = true;
    } else if (!isEmailValid(email)) {
    mEmailView.setError(getString(R.string.error_invalid_email));
    focusView = mEmailView;
    cancel = true;
    }
    if (cancel) {
    focusView.requestFocus();
    } else {
    showProgress(true);
    mAuthTask = new UserLoginTask(email, password);
    mAuthTask.execute((Void) null);
    }
    }
    Faaaaaaaaaaaat Controller!!

    View Slide

  8. Fat Controller is bad because...
    • Slow iteration
    • Ambiguous responsibilities
    • Off-by-one errors
    • Un-reproducible bugs

    View Slide

  9. Unit testing lets you...
    • Faster execution
    • Clear division of roles
    • Chance to improve architecture

    View Slide

  10. Is unit testing just enough?

    View Slide

  11. No!!
    It just ensures it partially worked

    View Slide

  12. See also...
    • EspressoςετίʔυͷಉظॲཧΛڀΊΔ
    • Day1 Room5 15:40 -
    • UIςετͷ࣮ߦ࣌ؒΛ୹ॖͤ͞Δํ๏
    • Day2 Room2 18:30 -

    View Slide

  13. anyway

    View Slide

  14. JUnit4 and basic assertions

    View Slide

  15. JUnit4 is...
    • A simple testing framework
    • Available for Android out-of-the-box
    • Still de-facto standard
    Moving Forward with JUnit 5 - Day2 Room5 10:30 -
    Check also

    View Slide

  16. Assertions

    View Slide

  17. Assertion is...
    • Assert something is something
    A rose is a rose is a rose is a rose...
    assertThat(1 + 1).isEqualTo(2);
    assertThat("Alice")
    .isNotEmpty()
    .isNotEqualTo("Bob");

    View Slide

  18. Installation
    dependencies {
    testImplementation 'junit:junit:4.12'
    testImplementation 'com.squareup.assertj:assertj-android:1.2.0'
    }
    assertj-android lets you assert Android framework's code as well

    View Slide

  19. Test target
    class InputChecker {
    boolean isValid(String text) {
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }
    VERY SIMPLE input text checker

    View Slide

  20. Create New Test
    Command + SHIFT + T

    View Slide

  21. Create New Test

    View Slide

  22. Create New Test

    View Slide

  23. Types of test in Android
    • Local Unit Tests
    • Runs on JVM
    • src/test
    • Instrumented Tests
    • Runs with Device/Emulators
    • src/androidTest

    View Slide

  24. Local Unit Tests: pros
    • Runs fast!
    • No device, emulator, android sdk

    View Slide

  25. Local Unit Tests: cons
    • No REAL Android framework's code

    View Slide

  26. • TRUE Android Test
    • Exactly the same behavior as production
    Instrumented Tests: pros

    View Slide

  27. Instrumented Tests: cons
    • Runs slow!
    • Need device, emulator, android sdk

    View Slide

  28. This session only cares
    Local Unit Tests

    View Slide

  29. Test class
    public class InputCheckerTest {
    @Before
    public void setUp() throws Exception {
    }
    @Test
    public void isValid() throws Exception {
    }
    }

    View Slide

  30. Test class
    public class InputCheckerTest {
    @Before
    public void setUp() throws Exception {
    }
    @Test
    public void isValid() throws Exception {
    }
    }
    Just naming convention

    View Slide

  31. Test class
    public class InputCheckerTest {
    @Before
    public void setUp() throws Exception {
    }
    @Test
    public void isValid() throws Exception {
    }
    }
    Called at every test case

    View Slide

  32. Test class
    public class InputCheckerTest {
    @Before
    public void setUp() throws Exception {
    }
    @Test
    public void isValid() throws Exception {
    }
    }
    This is a test case
    Write as many as you want

    View Slide

  33. Test class
    public class InputCheckerTest {
    @Before
    public void setUp() throws Exception {
    }foo
    @Test
    public void isValid() throws Exception {
    }foo
    }foo

    View Slide

  34. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }foo
    @Test
    public void isValid() throws Exception {
    }foo
    }foo

    View Slide

  35. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test
    public void isValid() throws Exception {
    }
    }
    Initialization codes here

    View Slide

  36. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }foo
    @Test
    public void isValid() throws Exception {
    }foo
    }foo

    View Slide

  37. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }foo
    @Test
    public void isValid() throws Exception {
    String input = "voodoo";
    boolean actual = inputChecker.isValid(input);
    Assertions.assertThat(actual).isTrue();
    }foo
    }foo

    View Slide

  38. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test
    public void isValid() throws Exception {
    String input = "voodoo";
    boolean actual = inputChecker.isValid(input);
    Assertions.assertThat(actual).isTrue();
    }
    }
    Write your assertions

    View Slide

  39. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test
    public void isValid() throws Exception {
    String input = "voodoo";
    boolean actual = inputChecker.isValid(input);
    Assertions.assertThat(actual).isTrue();
    }
    }
    what really is
    "actual"

    View Slide

  40. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test
    public void isValid() throws Exception {
    String input = "voodoo";
    boolean actual = inputChecker.isValid(input);
    Assertions.assertThat(actual).isTrue();
    }
    }
    "expectation"
    what you expect it to be

    View Slide

  41. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test
    public void isValid() throws Exception {
    String input = "voodoo";
    boolean actual = inputChecker.isValid(input);
    Assertions.assertThat(actual).isTrue();
    }
    }
    From AssertJ
    Many assertion methods available

    View Slide

  42. Test class
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test
    public void isValid() throws Exception {
    String input = "voodoo";
    boolean actual = inputChecker.isValid(input);
    Assertions.assertThat(actual).isTrue();
    }
    }

    View Slide

  43. Run tests
    Run single
    Run all
    or
    Passed!!!!

    View Slide

  44. If it fails...
    reason why it failed
    failure...
    Fix and try again!!!

    View Slide

  45. Congrats!!!

    View Slide

  46. Assertions (AssertJ)
    • assertThat(all)
    • isEqualTo / isNotEqualTo
    • assertThat(String)
    • isEmpty / isNotEmpty
    • assertThat(Number)
    • isPositive / isNegative / isZero / isNotZero
    • isGreaterThan / isLessThan
    lots and lots and lots more

    View Slide

  47. • assertThat(list)
    • contains / containsOnly / containsExactly
    Assertions (AssertJ)
    lots and lots and lots more
    List strings = Arrays.asList("foo", "bar", "baz");
    assertThat(strings).contains("bar");
    same collection
    same collection and order

    View Slide

  48. • http://joel-costigliola.github.io/assertj/
    • https://github.com/square/assertj-android
    • https://qiita.com/opengl-8080/items/
    b07307ab0d33422be9c5
    AssertJ

    View Slide

  49. Exceptions

    View Slide

  50. Remember this...
    class InputChecker {
    boolean isValid(String text) {
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }

    View Slide

  51. Remember this...
    class InputChecker {
    boolean isValid(String text) {
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }
    What if null...
    Let's try it anyway

    View Slide

  52. Ouch!!
    @Test
    public void isValid() throws Exception {
    String input = null;
    boolean actual = inputChecker.isValid(input);
    assertThat(actual).isTrue();
    }

    View Slide

  53. Expected Exception
    @Test(expected = NullPointerException.class)
    public void isValid() throws Exception {
    String input = null;
    boolean actual = inputChecker.isValid(input);
    assertThat(actual).isTrue();
    }

    View Slide

  54. Expected Exception
    @Test(expected = NullPointerException.class)
    public void isValid() throws Exception {
    String input = null;
    boolean actual = inputChecker.isValid(input);
    assertThat(actual).isTrue();
    }

    View Slide

  55. Passed!!
    @Test(expected = NullPointerException.class)
    public void isValid() throws Exception {
    String input = null;
    boolean actual = inputChecker.isValid(input);
    assertThat(actual).isTrue();
    }

    View Slide

  56. Better yet...
    class InputChecker {
    boolean isValid(String text) {
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }foo
    }foo

    View Slide

  57. Better yet...
    class InputChecker {
    boolean isValid(@NonNull String text) {
    if (text == null) throw new IllegalArgumentException("cannot be null");
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }foo
    }foo

    View Slide

  58. Better yet...
    class InputChecker {
    boolean isValid(@NonNull String text) {
    if (text == null) throw new IllegalArgumentException("cannot be null");
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }

    View Slide

  59. Better yet...
    class InputChecker {
    boolean isValid(@NonNull String text) {
    if (text == null) throw new IllegalArgumentException("cannot be null");
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }
    Unit testing helps you refactoring!!

    View Slide

  60. Way better!!
    @Test(expected = IllegalArgumentException.class)
    public void isValid() throws Exception {
    String input = null;
    boolean actual = inputChecker.isValid(input);
    assertThat(actual).isTrue();
    }

    View Slide

  61. Mock and spy with Mockito

    View Slide

  62. Mock is...
    • A WHOLE imitation of a real object
    • Used to simulate the behavior of
    depending objects

    View Slide

  63. Spy is...
    • A PARTIAL imitation of a real object
    • Used to simulate the behavior of
    depending objects

    View Slide

  64. Test Doubles
    They are

    View Slide

  65. Why you need mocks?

    View Slide

  66. TweetRepository
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }

    View Slide

  67. TweetRepository
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }
    depending on local data source

    View Slide

  68. Data Source
    public class LocalTweetDataSource {
    public List getTimeline() {
    List tweets = new ArrayList<>();
    // some local DB interaction
    // .....
    return tweets;
    }
    }

    View Slide

  69. Data Source
    public class LocalTweetDataSource {
    public List getTimeline() {
    List tweets = new ArrayList<>();
    // some local DB interaction
    // .....
    return tweets;
    }
    }
    Because it dose so many interactions with DB
    You don't wanna deal with all those messes!

    View Slide

  70. TweetRepository
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }
    You can replace this with simple mock

    View Slide

  71. Mockito

    View Slide

  72. Mockito is...
    • A simple mocking library
    • Most used one in Java and Android
    community

    View Slide

  73. Installation
    dependencies {
    testImplementation 'org.mockito:mockito-inline:2.13.0'
    }
    mockito-inline lets you mock final class & methods
    this artifact may be integrated into default in the future
    but you can't mock
    static methods & constructors

    View Slide

  74. Stubbing

    View Slide

  75. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );

    View Slide

  76. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );

    View Slide

  77. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );

    View Slide

  78. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );
    mock instance

    View Slide

  79. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn();
    stub method

    View Slide

  80. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn();

    View Slide

  81. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn();

    View Slide

  82. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn();

    View Slide

  83. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    );zz

    View Slide

  84. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );zz

    View Slide

  85. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );zz
    any item you wanna return

    View Slide

  86. Stubbing
    import static org.mockito.Mockito.*;
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );

    View Slide

  87. Stubbing
    private TweetRepository tweetRepository;
    @Before
    public void setUp() throws Exception {
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );
    tweetRepository = new TweetRepository(localDataSource);
    }

    View Slide

  88. Stubbing
    private TweetRepository tweetRepository;
    @Before
    public void setUp() throws Exception {
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    )
    );
    tweetRepository = new TweetRepository(localDataSource);
    }

    View Slide

  89. Stubbing
    @Test
    public void getTimeline() throws Exception {
    List tweets = tweetRepository.getTimeline();
    assertThat(tweets)
    .isNotEmpty()
    .hasSize(3)
    .containsExactly(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    );
    }

    View Slide

  90. Stubbing
    @Test
    public void getTimeline() throws Exception {
    List tweets = tweetRepository.getTimeline();
    assertThat(tweets)
    .isNotEmpty()
    .hasSize(3)
    .containsExactly(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    );
    }

    View Slide

  91. Stubbing
    @Test
    public void getTimeline() throws Exception {
    List tweets = tweetRepository.getTimeline();
    assertThat(tweets)
    .isNotEmpty()
    .hasSize(3)
    .containsExactly(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    );
    }

    View Slide

  92. Stubbing
    @Test
    public void getTimeline() throws Exception {
    List tweets = tweetRepository.getTimeline();
    assertThat(tweets)
    .isNotEmpty()
    .hasSize(3)
    .containsExactly(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    );
    }

    View Slide

  93. Stubbing
    @Test
    public void getTimeline() throws Exception {
    List tweets = tweetRepository.getTimeline();
    assertThat(tweets)
    .isNotEmpty()
    .hasSize(3)
    .containsExactly(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")
    );
    }
    pass!!

    View Slide

  94. Spying

    View Slide

  95. Spying is...
    • Stub methods can be added on a REAL
    object
    That means spied objects acts EXACTLY the same
    as real objects unless stubbed

    View Slide

  96. Spying
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  97. Spying
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());
    normal ArrayList

    View Slide

  98. Spying
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());
    wrap with spy()

    View Slide

  99. Spying
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());
    things you typically to with List

    View Slide

  100. Verify
    • Check the interactions with mock/spy
    mocks/spies will remember all interactions

    View Slide

  101. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  102. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  103. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  104. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  105. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  106. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  107. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());
    notice!

    View Slide

  108. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );abc
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  109. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());

    View Slide

  110. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());
    no interaction

    View Slide

  111. Verify
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    verify(strings, times(1)).addAll(
    eq(Arrays.asList("pop", "team", "epic"))
    );
    verify(strings, never()).add(eq("pop"));
    verify(strings, never()).get(anyInt());
    argument matcher

    View Slide

  112. Argument matchers
    • eq(T)
    • anyString()
    • anyInt()
    • any()
    • any(Class)
    • nullable(Class) null or T
    including null

    View Slide

  113. Stubbing with callbacks
    List strings = spy(new ArrayList());
    String[] array = {"pop", "team", "epic"};
    strings.addAll(asList(array));
    when(strings.get(anyInt())).thenAnswer(invocation -> {
    Object[] arguments = invocation.getArguments();
    int index = (int) arguments[0];
    if (index < array.length) return array[index];
    return "something-else";
    });
    String got = strings.get(123);
    assertThat(got).isEqualTo("something-else");
    if you wanna change return val
    according to the arguments

    View Slide

  114. Stubbing with callbacks
    List strings = spy(new ArrayList());
    String[] array = {"pop", "team", "epic"};
    strings.addAll(asList(array));
    when().thenAnswer(invocation -> {
    });

    View Slide

  115. Stubbing with callbacks
    List strings = spy(new ArrayList());
    String[] array = {"pop", "team", "epic"};
    strings.addAll(asList(array));
    when(strings.get(anyInt())).thenAnswer(invocation -> {
    Object[] arguments = invocation.getArguments();
    int index = (int) arguments[0];
    if (index < array.length) return array[index];
    return "something-else";
    });
    String got = strings.get(123);
    assertThat(got).isEqualTo("something-else");

    View Slide

  116. Stubbing with callbacks
    List strings = spy(new ArrayList());
    String[] array = {"pop", "team", "epic"};
    strings.addAll(asList(array));
    when(strings.get(anyInt())).thenAnswer(invocation -> {
    Object[] arguments = invocation.getArguments();
    int index = (int) arguments[0];
    if (index < array.length) return array[index];
    return "something-else";
    });
    String got = strings.get(123);
    assertThat(got).isEqualTo("something-else");

    View Slide

  117. Stubbing with callbacks
    List strings = spy(new ArrayList());
    String[] array = {"pop", "team", "epic"};
    strings.addAll(asList(array));
    when(strings.get(anyInt())).thenAnswer(invocation -> {
    Object[] arguments = invocation.getArguments();
    int index = (int) arguments[0];
    if (index < array.length) return array[index];
    return "something-else";
    });
    String got = strings.get(123);
    assertThat(got).isEqualTo("something-else");

    View Slide

  118. Stubbing with callbacks
    List strings = spy(new ArrayList());
    String[] array = {"pop", "team", "epic"};
    strings.addAll(asList(array));
    when(strings.get(anyInt())).thenAnswer(invocation -> {
    Object[] arguments = invocation.getArguments();
    int index = (int) arguments[0];
    if (index < array.length) return array[index];
    return "something-else";
    });
    String got = strings.get(123);
    assertThat(got).isEqualTo("something-else");
    whatever logic you like

    View Slide

  119. Stubbing with callbacks
    List strings = spy(new ArrayList());
    String[] array = {"pop", "team", "epic"};
    strings.addAll(asList(array));
    when(strings.get(anyInt())).thenAnswer(invocation -> {
    Object[] arguments = invocation.getArguments();
    int index = (int) arguments[0];
    if (index < array.length) return array[index];
    return "something-else";
    });
    String got = strings.get(123);
    assertThat(got).isEqualTo("something-else");
    Don't abuse this since it spoils simplicity

    View Slide

  120. Stubbing spy method

    View Slide

  121. doReturn
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    when(strings.get(3)).thenReturn("foo");
    when().thenReturn() is almost always useful

    View Slide

  122. doReturn
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    when(strings.get(3)).thenReturn("foo");
    0 1 2
    Because this is a spy, not mock,
    List#get() is evaluated first that causes an Exception

    View Slide

  123. doReturn
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    doReturn("foo").when(strings).get(3);
    String got = strings.get(3);
    Assertions.assertThat(got).isEqualTo("foo");

    View Slide

  124. doReturn
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    doReturn("foo").when(strings).get(3);
    String got = strings.get(3);
    Assertions.assertThat(got).isEqualTo("foo");

    View Slide

  125. doReturn
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    doReturn("foo").when(strings).get(3);
    String got = strings.get(3);
    Assertions.assertThat(got).isEqualTo("foo");

    View Slide

  126. doReturn
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    doReturn("foo").when(strings).get(3);
    String got = strings.get(3);
    Assertions.assertThat(got).isEqualTo("foo");

    View Slide

  127. doReturn
    List strings = spy(new ArrayList());
    strings.addAll(Arrays.asList("pop", "team", "epic"));
    strings.forEach(System.out::println);
    doReturn("foo").when(strings).get(3);
    String got = strings.get(3);
    Assertions.assertThat(got).isEqualTo("foo");
    This works well!

    View Slide

  128. Stubbing void method

    View Slide

  129. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    strings.clear();

    View Slide

  130. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    strings.clear();

    View Slide

  131. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    strings.clear();

    View Slide

  132. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    strings.clear();

    View Slide

  133. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    strings.clear();

    View Slide

  134. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    strings.clear();

    View Slide

  135. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    doNothing().when(strings).clear();
    strings.clear();

    View Slide

  136. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    doNothing().when(strings).clear();
    strings.clear();

    View Slide

  137. doThrow / doNothing
    List strings = spy(new ArrayList());
    doThrow(new IllegalStateException()).when(strings).clear();
    doNothing().when(strings).clear();
    strings.clear(); pass!!

    View Slide

  138. • http://site.mockito.org/
    • http://static.javadoc.io/org.mockito/mockito-core/
    2.13.0/org/mockito/Mockito.html#0
    Mockito
    REALLY HELPFUL!!

    View Slide

  139. Robolectric for framework's code

    View Slide

  140. InputChecker
    class InputChecker {
    boolean isValid(@NonNull String text) {
    if (text == null)
    throw new IllegalArgumentException("cannot be null");
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }

    View Slide

  141. InputChecker
    class InputChecker {
    boolean isValid(@NonNull String text) {
    if (TextUtils.isEmpty(text))
    throw new IllegalArgumentException("cannot be blank");
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }

    View Slide

  142. InputChecker
    class InputChecker {
    boolean isValid(@NonNull String text) {
    if (TextUtils.isEmpty(text))
    throw new IllegalArgumentException("cannot be blank");
    if (text.length() < 4) return false;
    if (!text.matches("[a-zA-Z0-9]+")) return false;
    return true;
    }
    }
    android.text.TextUtils

    View Slide

  143. InputCheckerTest
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test
    public void isValid() throws Exception {
    String input = "voodoo";
    boolean actual = inputChecker.isValid(input);
    assertThat(actual).isTrue();
    }
    }

    View Slide

  144. InputCheckerTest
    Basically, Android framework's code cannot be executed
    on JVM tests

    View Slide

  145. app/build.gradle
    android {
    testOptions {
    unitTests.returnDefaultValues = true
    }
    } notice: this setting just return the default value
    @Test isValid() passes successfully

    View Slide

  146. InputCheckerTest
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test(expected = IllegalArgumentException.class)
    public void isValid_fails_with_blank() throws Exception {
    String input = "";
    inputChecker.isValid(input);
    }
    } Add a test case that checks blank input

    View Slide

  147. InputCheckerTest
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test(expected = IllegalArgumentException.class)
    public void isValid_fails_with_blank() throws Exception {
    String input = "";
    inputChecker.isValid(input);
    }
    }
    Expected exception was not thrown!
    This is because of the "default value" from test options

    View Slide

  148. Robolectric

    View Slide

  149. Robolectric is...
    • "A unit test framework that de-fangs the
    Android SDK jar so you can test-drive the
    development of your Android app."
    • "Tests run inside the JVM on your
    workstation in seconds."
    quoted from the official site

    View Slide

  150. testImplementation "org.robolectric:robolectric:3.6.1"
    android {
    testOptions {
    unitTests {
    includeAndroidResources = true
    }
    }
    }
    Installation
    remove: unitTests.returnDefaultValues = true

    View Slide

  151. InputCheckerTest
    @RunWith(RobolectricTestRunner.class)
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test(expected = IllegalArgumentException.class)
    public void isValid_fails_with_blank() throws Exception {
    String input = "";
    inputChecker.isValid(input);
    }
    }

    View Slide

  152. InputCheckerTest
    @RunWith(RobolectricTestRunner.class)
    public class InputCheckerTest {
    private InputChecker inputChecker;
    @Before
    public void setUp() throws Exception {
    inputChecker = new InputChecker();
    }
    @Test(expected = IllegalArgumentException.class)
    public void isValid_fails_with_blank() throws Exception {
    String input = "";
    inputChecker.isValid(input);
    }
    }
    Just add this!!

    View Slide

  153. Context in Robolectric
    @RunWith(RobolectricTestRunner.class)
    public class SQLiteTest {
    private SQLiteDatabase db;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    SQLiteOpenHelper helper = new MySQLiteOpenHelper(context);
    db = helper.getWritableDatabase();
    }
    @Test
    public void test_db() throws Exception {
    // do the stuff
    }
    }

    View Slide

  154. Context in Robolectric
    @RunWith(RobolectricTestRunner.class)
    public class SQLiteTest {
    private SQLiteDatabase db;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    SQLiteOpenHelper helper = new MySQLiteOpenHelper(context);
    db = helper.getWritableDatabase();
    }
    @Test
    public void test_db() throws Exception {
    // do the stuff
    }
    }
    Really handy to use!!

    View Slide

  155. • http://robolectric.org/
    • http://robolectric.org/getting-started/
    • http://robolectric.org/extending/
    Robolectric
    One thing you have to notice about Robolectric is
    It just mimics the framework's behavior, not the real one

    View Slide

  156. Unit Testing SQLite

    View Slide

  157. • Robolectric simulates SQLite out-of-the-box!!
    • You can use traditional SQLiteOpenHelper
    • You can also use the latest Room Persistence Library
    Unit Testing SQLite

    View Slide

  158. Room Persistence Library
    dependencies {
    implementation "android.arch.persistence.room:runtime:1.0.0"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
    }

    View Slide

  159. Room Persistence Library
    @Entity
    public class User {
    @PrimaryKey
    private int uid;
    @ColumnInfo(name = "first_name")
    private String firstName;
    @ColumnInfo(name = "last_name")
    private String lastName;
    public User(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    // getters & setters
    }
    Entity

    View Slide

  160. Room Persistence Library
    @Dao
    public interface UserDao {
    @Query("SELECT * FROM user")
    List getAll();
    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
    List loadAllByIds(int[] userIds);
    @Query("SELECT * FROM user WHERE first_name LIKE :first AND "
    + "last_name LIKE :last LIMIT 1")
    User findByName(String first, String last);
    @Insert
    void insertAll(User... users);
    @Delete
    void delete(User user);
    }
    Dao

    View Slide

  161. Room Persistence Library
    @Database(entities = {User.class}, version = 1)
    public abstract class AppDatabase extends RoomDatabase {
    public abstract UserDao userDao();
    }
    Database

    View Slide

  162. Room Persistence Library
    public class UserManager {
    private final String DB_NAME = "sample_app";
    private final AppDatabase db;
    public UserManager(@NonNull Context context) {
    db = Room.databaseBuilder(context.getApplicationContext(),
    AppDatabase.class, DB_NAME)
    .allowMainThreadQueries()
    .build();
    }
    public void insertUser(@NonNull User user) {
    db.userDao().insertAll(user);
    }
    public List getUsers() {
    return db.userDao().getAll();
    }
    }
    Business Logic

    View Slide

  163. Room Persistence Library
    public class UserManager {
    private final String DB_NAME = "sample_app";
    private final AppDatabase db;
    public UserManager(@NonNull Context context) {
    db = Room.databaseBuilder(context.getApplicationContext(),
    AppDatabase.class, DB_NAME)
    .allowMainThreadQueries()
    .build();
    }
    public void insertUser(@NonNull User user) {
    db.userDao().insertAll(user);
    }
    public List getUsers() {
    return db.userDao().getAll();
    }
    }
    Business Logic
    just for demo's convenience

    View Slide

  164. Room Persistence Library
    public class UserManager {
    private final String DB_NAME = "sample_app";
    private final AppDatabase db;
    public UserManager(@NonNull Context context) {
    db = Room.databaseBuilder(context.getApplicationContext(),
    AppDatabase.class, DB_NAME)
    .allowMainThreadQueries()
    .build();
    }
    public void insertUser(@NonNull User user) {
    db.userDao().insertAll(user);
    }
    public List getUsers() {
    return db.userDao().getAll();
    }
    }
    Business Logic
    delegate

    View Slide

  165. Room Persistence Library
    public class UserManager {
    private final String DB_NAME = "sample_app";
    private final AppDatabase db;
    public UserManager(@NonNull Context context) {
    db = Room.databaseBuilder(context.getApplicationContext(),
    AppDatabase.class, DB_NAME)
    .allowMainThreadQueries()
    .build();
    }
    public void insertUser(@NonNull User user) {
    db.userDao().insertAll(user);
    }
    public List getUsers() {
    return db.userDao().getAll();
    }
    }
    Business Logic
    delegate

    View Slide

  166. Room Persistence Library
    @RunWith(RobolectricTestRunner.class)
    public class UserManagerTest {
    private UserManager userManager;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    userManager = new UserManager(context);
    }
    @Test
    public void insert_and_get() throws Exception {
    List users = userManager.getUsers();
    assertThat(users).isEmpty();
    userManager.insertUser(new User("Fumihiko", "Shiroyama"));
    users = userManager.getUsers();
    assertThat(users)
    .isNotEmpty()
    .hasSize(1);
    }
    }

    View Slide

  167. Room Persistence Library
    @RunWith(RobolectricTestRunner.class)
    public class UserManagerTest {
    private UserManager userManager;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    userManager = new UserManager(context);
    }
    @Test
    public void insert_and_get() throws Exception {
    List users = userManager.getUsers();
    assertThat(users).isEmpty();
    userManager.insertUser(new User("Fumihiko", "Shiroyama"));
    users = userManager.getUsers();
    assertThat(users)
    .isNotEmpty()
    .hasSize(1);
    }
    }

    View Slide

  168. Room Persistence Library
    @RunWith(RobolectricTestRunner.class)
    public class UserManagerTest {
    private UserManager userManager;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    userManager = new UserManager(context);
    }
    @Test
    public void insert_and_get() throws Exception {
    List users = userManager.getUsers();
    assertThat(users).isEmpty();
    userManager.insertUser(new User("Fumihiko", "Shiroyama"));
    users = userManager.getUsers();
    assertThat(users)
    .isNotEmpty()
    .hasSize(1);
    }
    }

    View Slide

  169. Room Persistence Library
    @RunWith(RobolectricTestRunner.class)
    public class UserManagerTest {
    private UserManager userManager;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    userManager = new UserManager(context);
    }
    @Test
    public void insert_and_get() throws Exception {
    List users = userManager.getUsers();
    assertThat(users).isEmpty();
    userManager.insertUser(new User("Fumihiko", "Shiroyama"));
    users = userManager.getUsers();
    assertThat(users)
    .isNotEmpty()
    .hasSize(1);
    }
    }

    View Slide

  170. Room Persistence Library
    @RunWith(RobolectricTestRunner.class)
    public class UserManagerTest {
    private UserManager userManager;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    userManager = new UserManager(context);
    }
    @Test
    public void insert_and_get() throws Exception {
    List users = userManager.getUsers();
    assertThat(users).isEmpty();
    userManager.insertUser(new User("Fumihiko", "Shiroyama"));
    users = userManager.getUsers();
    assertThat(users)
    .isNotEmpty()
    .hasSize(1);
    }
    }

    View Slide

  171. Room Persistence Library
    @RunWith(RobolectricTestRunner.class)
    public class UserManagerTest {
    private UserManager userManager;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    userManager = new UserManager(context);
    }
    @Test
    public void insert_and_get() throws Exception {
    List users = userManager.getUsers();
    assertThat(users).isEmpty();
    userManager.insertUser(new User("Fumihiko", "Shiroyama"));
    users = userManager.getUsers();
    assertThat(users)
    .isNotEmpty()
    .hasSize(1);
    }
    }

    View Slide

  172. Room Persistence Library
    @RunWith(RobolectricTestRunner.class)
    public class UserManagerTest {
    private UserManager userManager;
    @Before
    public void setUp() throws Exception {
    Context context
    = RuntimeEnvironment.application.getApplicationContext();
    userManager = new UserManager(context);
    }
    @Test
    public void insert_and_get() throws Exception {
    List users = userManager.getUsers();
    assertThat(users).isEmpty();
    userManager.insertUser(new User("Fumihiko", "Shiroyama"));
    users = userManager.getUsers();
    assertThat(users)
    .isNotEmpty()
    .hasSize(1);
    }
    }
    DB & table's data are reset automatically

    View Slide

  173. Room Persistence Library
    • https://developer.android.com/topic/
    libraries/architecture/room.html
    • https://developer.android.com/topic/
    libraries/architecture/adding-
    components.html
    • https://developer.android.com/training/
    data-storage/room/index.html

    View Slide

  174. Unit Testing asynchronous code

    View Slide

  175. Testing async code
    • Testing asynchronous code is not always
    easy
    I'd introduce the very basic concept of it!

    View Slide

  176. Synchronous call
    interface Fetcher {
    T fetch();
    class StringFetcher implements Fetcher {
    @Override
    public String fetch() {
    return "OK";
    }
    }
    } Think of a synchronous API call over internet

    View Slide

  177. Simple callback
    Callback interfaces
    interface OnSuccess {
    void onSuccess(T result);
    }
    interface OnFailure {
    void onFailure(Exception e);
    }

    View Slide

  178. Asynchronous call
    class AsyncFetcher {
    private final Fetcher fetcher;
    AsyncFetcher(Fetcher fetcher) {
    this.fetcher = fetcher;
    }
    void fetch(OnSuccess onSuccess, OnFailure onFailure) {
    new Thread(() -> {
    try {
    Thread.sleep(1000L);
    onSuccess.onSuccess(fetcher.fetch());
    } catch (Exception e) {
    onFailure.onFailure(e);
    }
    }).start();
    }
    }
    Think of an asynchronous API call over internet

    View Slide

  179. Asynchronous call
    class AsyncFetcher {
    private final Fetcher fetcher;
    AsyncFetcher(Fetcher fetcher) {
    this.fetcher = fetcher;
    }
    void fetch(OnSuccess onSuccess, OnFailure onFailure) {
    new Thread(() -> {
    try {
    Thread.sleep(1000L);
    onSuccess.onSuccess(fetcher.fetch());
    } catch (Exception e) {
    onFailure.onFailure(e);
    }
    }).start();
    }
    }

    View Slide

  180. Asynchronous call
    class AsyncFetcher {
    private final Fetcher fetcher;
    AsyncFetcher(Fetcher fetcher) {
    this.fetcher = fetcher;
    }
    void fetch(OnSuccess onSuccess, OnFailure onFailure) {
    new Thread(() -> {
    try {
    Thread.sleep(1000L);
    onSuccess.onSuccess(fetcher.fetch());
    } catch (Exception e) {
    onFailure.onFailure(e);
    }
    }).start();
    }
    }

    View Slide

  181. Asynchronous call
    class AsyncFetcher {
    private final Fetcher fetcher;
    AsyncFetcher(Fetcher fetcher) {
    this.fetcher = fetcher;
    }
    void fetch(OnSuccess onSuccess, OnFailure onFailure) {
    new Thread(() -> {
    try {
    Thread.sleep(1000L);
    onSuccess.onSuccess(fetcher.fetch());
    } catch (Exception e) {
    onFailure.onFailure(e);
    }
    }).start();
    }
    }
    Executed on a different thread

    View Slide

  182. Asynchronous call
    class AsyncFetcher {
    private final Fetcher fetcher;
    AsyncFetcher(Fetcher fetcher) {
    this.fetcher = fetcher;
    }
    void fetch(OnSuccess onSuccess, OnFailure onFailure) {
    new Thread(() -> {
    try {
    Thread.sleep(1000L);
    onSuccess.onSuccess(fetcher.fetch());
    } catch (Exception e) {
    onFailure.onFailure(e);
    }
    }).start();
    }
    }
    time intensive task

    View Slide

  183. Asynchronous call
    class AsyncFetcher {
    private final Fetcher fetcher;
    AsyncFetcher(Fetcher fetcher) {
    this.fetcher = fetcher;
    }
    void fetch(OnSuccess onSuccess, OnFailure onFailure) {
    new Thread(() -> {
    try {
    Thread.sleep(1000L);
    onSuccess.onSuccess(fetcher.fetch());
    } catch (Exception e) {
    onFailure.onFailure(e);
    }
    }).start();
    }
    }
    Callbacks

    View Slide

  184. Asynchronous Test
    public class AsyncFetcherTest {
    private Fetcher fetcher;
    private AsyncFetcher asyncFetcher;
    @Before
    public void setUp() throws Exception {
    fetcher = spy(new Fetcher.StringFetcher());
    asyncFetcher = new AsyncFetcher<>(fetcher);
    }
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch();
    }
    }

    View Slide

  185. Asynchronous Test
    public class AsyncFetcherTest {
    private Fetcher fetcher;
    private AsyncFetcher asyncFetcher;
    @Before
    public void setUp() throws Exception {
    fetcher = spy(new Fetcher.StringFetcher());
    asyncFetcher = new AsyncFetcher<>(fetcher);
    }
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch();
    }
    }

    View Slide

  186. Asynchronous Test
    public class AsyncFetcherTest {
    private Fetcher fetcher;
    private AsyncFetcher asyncFetcher;
    @Before
    public void setUp() throws Exception {
    fetcher = spy(new Fetcher.StringFetcher());
    asyncFetcher = new AsyncFetcher<>(fetcher);
    }
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch();
    }
    }
    How do I write this test?

    View Slide

  187. Asynchronous Test
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch(
    result -> {
    assertThat(result).isEqualTo("OK");
    log("async: ok");
    },
    e -> log("async: ng")
    );
    log("fetch_success: end");
    }

    View Slide

  188. Asynchronous Test
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch(
    result -> {
    assertThat(result).isEqualTo("OK");
    log("async: ok");
    },
    e -> log("async: ng")
    );
    log("fetch_success: end");
    }
    success

    View Slide

  189. Asynchronous Test
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch(
    result -> {
    assertThat(result).isEqualTo("OK");
    log("async: ok");
    },
    e -> log("async: ng")
    );
    log("fetch_success: end");
    }
    failure

    View Slide

  190. Asynchronous Test
    @Test
    public void fetch_failure() throws Exception {
    doThrow(new RuntimeException("NG")).when(fetcher).fetch();
    asyncFetcher.fetch(
    result -> log("async: ok"),
    e -> {
    assertThat(e.getMessage()).isEqualTo("NG");
    log("async: ng");
    }
    );
    log("fetch_failure: end");
    }

    View Slide

  191. Asynchronous Test
    @Test
    public void fetch_failure() throws Exception {
    doThrow(new RuntimeException("NG")).when(fetcher).fetch();
    asyncFetcher.fetch(
    result -> log("async: ok"),
    e -> {
    assertThat(e.getMessage()).isEqualTo("NG");
    log("async: ng");
    }
    );
    log("fetch_failure: end");
    }
    stub failure

    View Slide

  192. Asynchronous Test
    Callback is NOT actually called!!!
    Execution seems successful...but wait?

    View Slide

  193. Asynchronous Test
    private CountDownLatch latch;
    @Before
    public void setUp() throws Exception {
    fetcher = spy(new Fetcher.StringFetcher());
    asyncFetcher = new AsyncFetcher<>(fetcher);
    latch = new CountDownLatch(1);
    }

    View Slide

  194. Asynchronous Test
    Acts like an ON/OFF latch
    private CountDownLatch latch;
    @Before
    public void setUp() throws Exception {
    fetcher = spy(new Fetcher.StringFetcher());
    asyncFetcher = new AsyncFetcher<>(fetcher);
    latch = new CountDownLatch(1);
    }

    View Slide

  195. Asynchronous Test
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch(
    result -> {
    assertThat(result).isEqualTo("OK");
    log("async: ok");
    latch.countDown();
    },
    e -> log("async: ng")
    );
    latch.await();
    log("fetch_success: end");
    }

    View Slide

  196. Asynchronous Test
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch(
    result -> {
    assertThat(result).isEqualTo("OK");
    log("async: ok");
    latch.countDown();
    },
    e -> log("async: ng")
    );
    latch.await();
    log("fetch_success: end");
    }

    View Slide

  197. Asynchronous Test
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch(
    result -> {
    assertThat(result).isEqualTo("OK");
    log("async: ok");
    latch.countDown();
    },
    e -> log("async: ng")
    );
    latch.await();
    log("fetch_success: end");
    }
    Test execution awaits here

    View Slide

  198. Asynchronous Test
    @Test
    public void fetch_success() throws Exception {
    asyncFetcher.fetch(
    result -> {
    assertThat(result).isEqualTo("OK");
    log("async: ok");
    latch.countDown();
    },
    e -> log("async: ng")
    );
    latch.await();
    log("fetch_success: end");
    }
    Opens the latch and test goes again

    View Slide

  199. Asynchronous Test
    @Test
    public void fetch_failure() throws Exception {
    doThrow(new RuntimeException("NG")).when(fetcher).fetch();
    asyncFetcher.fetch(
    result -> log("async: ok"),
    e -> {
    assertThat(e.getMessage()).isEqualTo("NG");
    log("async: ng");
    latch.countDown();
    }
    );
    latch.await();
    log("fetch_failure: end");
    }

    View Slide

  200. Asynchronous Test
    @Test
    public void fetch_failure() throws Exception {
    doThrow(new RuntimeException("NG")).when(fetcher).fetch();
    asyncFetcher.fetch(
    result -> log("async: ok"),
    e -> {
    assertThat(e.getMessage()).isEqualTo("NG");
    log("async: ng");
    latch.countDown();
    }
    );
    latch.await();
    log("fetch_failure: end");
    }
    The same implementation as success

    View Slide

  201. Asynchronous Test
    Callback is properly called!!!
    Log message seems correct!!

    View Slide

  202. Additional info...
    • MockWebServer for OkHttp
    • TestSubscriber for RxJava
    • https://speakerdeck.com/srym/
    hayaiyasuiumai-sutatoatupudemoshi-eru-
    retrofit-plus-rxjava-deshun-jian-
    apikutukinguresipi?slide=17
    Check this slide & sample project

    View Slide

  203. Best practices and tips

    View Slide

  204. Best practices and tips
    • Use interface at the structural boundary
    • Use DI pattern for testability
    • Static is EVIL
    • Don't be a Mad Tester

    View Slide

  205. Use interface
    at the structural boundary

    View Slide

  206. Repository pattern
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }

    View Slide

  207. Repository pattern
    public class LocalTweetDataSource {
    public List getTimeline() {
    List tweets = new ArrayList<>();
    // some local DB interaction
    return tweets;
    }
    }
    Concrete data source

    View Slide

  208. Repository pattern
    public interface LocalTweetDataSource {
    List getTimeline();
    class SQLiteTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    List tweets = new ArrayList<>();
    // some local DB interaction
    return tweets;
    }
    }
    class MockTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    return Arrays.asList(/* some mock data */);
    }
    }
    }

    View Slide

  209. Repository pattern
    public interface LocalTweetDataSource {
    List getTimeline();
    class SQLiteTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    List tweets = new ArrayList<>();
    // some local DB interaction
    return tweets;
    }
    }
    class MockTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    return Arrays.asList(/* some mock data */);
    }
    }
    }

    View Slide

  210. Repository pattern
    public interface LocalTweetDataSource {
    List getTimeline();
    class SQLiteTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    List tweets = new ArrayList<>();
    // some local DB interaction
    return tweets;
    }
    }
    class MockTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    return Arrays.asList(/* some mock data */);
    }
    }
    }
    Concrete data source with real DB interactions

    View Slide

  211. Repository pattern
    public interface LocalTweetDataSource {
    List getTimeline();
    class SQLiteTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    List tweets = new ArrayList<>();
    // some local DB interaction
    return tweets;
    }
    }
    class MockTweetDataSource implements LocalTweetDataSource {
    @Override
    public List getTimeline() {
    return Arrays.asList(/* some mock data */);
    }
    }
    }
    mock data source just for tests

    View Slide

  212. Repository pattern
    public class TweetRepositoryTest {
    private TweetRepository tweetRepository;
    @Before
    public void setUp() throws Exception {
    LocalTweetDataSource localDataSource = mock(LocalTweetDataSource.class);
    when(localDataSource.getTimeline()).thenReturn(
    Arrays.asList(
    Tweet.bodyOf("foo"),
    Tweet.bodyOf("bar"),
    Tweet.bodyOf("baz")));
    tweetRepository = new TweetRepository(localDataSource);
    }
    }

    View Slide

  213. Repository pattern
    public class TweetRepositoryTest {
    private TweetRepository tweetRepository;
    @Before
    public void setUp() throws Exception {
    LocalTweetDataSource localDataSource
    = new LocalTweetDataSource.MockTweetDataSource();
    tweetRepository = new TweetRepository(localDataSource);
    }
    } You can even get rid of Mockito dependency

    View Slide

  214. Use DI pattern for testability

    View Slide

  215. Use DI pattern
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository() {
    this.localDataSource = new LocalTweetDataSource.SQLiteTweetDataSource();
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }

    View Slide

  216. Use DI pattern
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository() {
    this.localDataSource = new LocalTweetDataSource.SQLiteTweetDataSource();
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }
    mocking field is painful!!

    View Slide

  217. Use DI pattern
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository() {
    this.localDataSource = new LocalTweetDataSource.SQLiteTweetDataSource();
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }

    View Slide

  218. Use DI pattern
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }

    View Slide

  219. Use DI pattern
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }
    Putting dependency from outer

    View Slide

  220. Use DI pattern
    • This is so-called DI pattern
    • DI pattern does NOT have to use DI library
    • DI library (e.g. Dagger2) is useful though
    e.g. Factory Pattern

    View Slide

  221. Static is EVIL

    View Slide

  222. Static is EVIL because...
    • Static has global scope
    • Mockito cannot mock static
    • Static is practically unnecessary
    This may cause order dependent bugs

    View Slide

  223. Static is practically unnecessary
    @Singleton
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    @Inject
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }

    View Slide

  224. Static is practically unnecessary
    @Singleton
    public class TweetRepository {
    private final LocalTweetDataSource localDataSource;
    @Inject
    public TweetRepository(LocalTweetDataSource localDataSource) {
    this.localDataSource = localDataSource;
    }
    public List getTimeline() {
    return localDataSource.getTimeline();
    }
    }
    Use Dagger's annotations for handy singleton

    View Slide

  225. Don't be a Mad Tester

    View Slide

  226. Don't be a Mad Tester
    • Avoid to test EVERYTHING
    • Unit tests are NOT perfect
    • Gain maximum with minimum investment!
    Enjoy your test life!!!

    View Slide

  227. Thank you for listening!
    • All illustrations by www.irasutoya.com
    • Special Thanks to @sumio_tym, @numa08
    • Get the slides: https://goo.gl/ju66No
    • Get the materials: https://goo.gl/R5kBeN

    View Slide