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

Android MV* Patterns & Unit Testing

Paresh Mayani
November 26, 2016

Android MV* Patterns & Unit Testing

"Android MV* Patterns & Unit Testing", talk delivered in DevFest Ahmedabad 2016 http://devfest.gdgahmedabad.com/

- Android MVC vs MVP vs MVVM
- Android Unit Testing
- Why Unit Testing
- Why Good code base
- Android Testing frameworks and tools
- Continuous Integration in Android

Paresh Mayani

November 26, 2016
Tweet

More Decks by Paresh Mayani

Other Decks in Technology

Transcript

  1. View Slide

  2. PareshMayani.apk
    Founder/Organizer, GDG Ahmedabad
    Technical Lead @ Lets Nurture
    Application Architect

    View Slide

  3. Good/Clean code base?
    What is that? Why should we have good/clean code base?

    View Slide

  4. Scalable

    View Slide

  5. Scalable | Stable

    View Slide

  6. Scalable | Stable | Testable

    View Slide

  7. Scalable | Stable | Testable | Modular

    View Slide

  8. Application Architecture?
    And why do I care?

    View Slide

  9. MV* Patterns?
    And why do I care?

    View Slide

  10. MV (C | P | VM)
    Model
    Business logic
    Data source of the application
    Network layer, database operations

    View Slide

  11. MV (C | P | VM)
    View
    Business logic
    Data source of the application
    Network layer, database operations
    Responsible for displaying data
    Model

    View Slide

  12. MVC
    Model View Controller

    View Slide

  13. View
    Model
    Controller
    Controller manipulates, edit, uses data model
    and show it to users via View.

    View Slide

  14. View
    Model
    Controller
    In Android, Activity/Fragments can act as both
    View and Controller

    View Slide

  15. MVP
    Model View Presenter

    View Slide

  16. Presenter
    Model
    View
    1 .. 1

    View Slide

  17. Presenter
    Model
    View
    1 .. 1
    Presenter is a simple java class that do not
    contain any UI components, it just manipulates
    data from model and display in on View.

    View Slide

  18. MVVM
    Model View ViewModel

    View Slide

  19. ViewModel
    Model
    View
    1 .. *
    Many View can be mapped to one View-Model

    View Slide

  20. MVC vs MVP vs MVVM
    Show me flow how it actually works

    View Slide

  21. Passes
    call to
    Fire
    events
    Manipulates
    Model-View-Controller
    Controller Model
    View
    User Interaction

    View Slide

  22. Passes
    call to
    Fire
    events
    Manipulates
    Model-View-Controller
    Controller Model
    View
    User Interaction
    Passes
    call to
    Manipulates
    Model-View-Presenter
    Presenter
    Model
    View
    Updates
    User Interaction
    Fire events

    View Slide

  23. Passes
    call to
    Fire
    events
    Manipulates
    Model-View-Controller
    Controller Model
    View
    User Interaction
    Passes
    call to
    Manipulates
    Model-View-Presenter
    Presenter
    Model
    View Bi-directional
    Data Binding
    Manipulates
    Model-View-ViewModel
    ViewModel
    Model
    View
    Updates
    User Interaction User Interaction
    Fire
    events
    Fire events

    View Slide

  24. MVC vs MVP vs MVVM
    Which one to follow?

    View Slide

  25. Activity/Fragment/View
    should be Business logic free

    View Slide

  26. Can you unit test everything?

    View Slide

  27. Unit Testing
    Why to test?

    View Slide

  28. Local unit tests
    Instrumented tests
    Test Types

    View Slide

  29. Located at module-name/src/test/java/.
    These tests run on the local JVM and do not have
    access to functional Android framework APIs.
    Local unit tests

    View Slide

  30. Located at module-name/src/androidTest/java/.
    These are all tests that must run on an Android
    hardware device or an Android emulator.
    Instrumented (end-to-end) tests

    View Slide

  31. 1) Instrumented test 2) Local JVM Test

    View Slide

  32. Why Unit Testing?
    Why should we write unit test?

    View Slide

  33. “Unit Testing is a level of software testing where
    individual units/ components of a software are tested. “
    The purpose is to validate that each unit of the software
    performs as designed.”
    All possible scenarios

    View Slide

  34. Unit tests are for the software developers
    Unit tests are written by software developers

    View Slide

  35. Options: JUnit, Robolectric
    Frameworks: Mockito, PowerMock
    Unit Testing

    View Slide

  36. Mocking => @mock
    @spy
    Stubbing => when().thenreturn(true/false)
    Verifying => verify()
    Mockito

    View Slide

  37. // You can mock concrete classes and interfaces
    TrainSeats seats = mock(TrainSeats.class);
    // stubbing appears before the actual execution
    when(seats.book(Seat.near(WINDOW).in(FIRST_CLASS))).thenReturn(BOOKED);
    // the following prints "BOOKED"
    System.out.println(seats.book(Seat.near(WINDOW).in(FIRST_CLASS)));
    // the following prints "null" because .book(Seat.near(AISLE).in(FIRST_CLASS))) was not stubbed
    System.out.println(seats.book(Seat.near(AISLE).in(FIRST_CLASS)));
    // the following verification passes because .book(Seat.near(WINDOW).in(FIRST_CLASS)) has been
    invoked
    verify(seats).book(Seat.near(WINDOW).in(FIRST_CLASS))
    // the following verification fails because .book(Seat.in(SECOND_CLASS)) has not been invoked
    verify(seats).book(Seat.in(SECOND_CLASS))
    Example

    View Slide

  38. Options: Robotium, Espresso
    Firebase Test lab support
    Android Studio test recorder
    Instrumentation tests

    View Slide

  39. View Slide

  40. View Slide

  41. Continuous Integration
    Can I perform unit testing automatically whenever software builds?

    View Slide

  42. Jenkins (Open source)
    Travis / Circle CI / Atlanssian Bamboo
    Performs testing on triggering actions like PR merge
    Automated reporting: Send email if build fails
    Auto publishing to play store
    Continuous Integration

    View Slide

  43. Demo Time
    MVP Pattern & Unit Testing

    View Slide

  44. https://goo.gl/Z4dy8G
    https://github.com/googlesamples/android-architecture
    Resources

    View Slide

  45. No Questions!

    View Slide

  46. Thank you!
    Paresh Mayani
    @pareshmayani
    Grab This Presentation
    https://goo.gl/Xsvpi3

    View Slide