Android MV* Patterns & Unit Testing

2c9f04076de133db434c7d9966ac6f6b?s=47 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

2c9f04076de133db434c7d9966ac6f6b?s=128

Paresh Mayani

November 26, 2016
Tweet

Transcript

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

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

    good/clean code base?
  4. Scalable

  5. Scalable | Stable

  6. Scalable | Stable | Testable

  7. Scalable | Stable | Testable | Modular

  8. Application Architecture? And why do I care?

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

  10. MV (C | P | VM) Model Business logic Data

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

    source of the application Network layer, database operations Responsible for displaying data Model
  12. MVC Model View Controller

  13. View Model Controller Controller manipulates, edit, uses data model and

    show it to users via View.
  14. View Model Controller In Android, Activity/Fragments can act as both

    View and Controller
  15. MVP Model View Presenter

  16. Presenter Model View 1 .. 1

  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.
  18. MVVM Model View ViewModel

  19. ViewModel Model View 1 .. * Many View can be

    mapped to one View-Model
  20. MVC vs MVP vs MVVM Show me flow how it

    actually works
  21. Passes call to Fire events Manipulates Model-View-Controller Controller Model View

    User Interaction
  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
  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
  24. MVC vs MVP vs MVVM Which one to follow?

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

  26. Can you unit test everything?

  27. Unit Testing Why to test?

  28. Local unit tests Instrumented tests Test Types

  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
  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
  31. 1) Instrumented test 2) Local JVM Test

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

  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
  34. Unit tests are for the software developers Unit tests are

    written by software developers
  35. Options: JUnit, Robolectric Frameworks: Mockito, PowerMock Unit Testing

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

    Mockito
  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
  38. Options: Robotium, Espresso Firebase Test lab support Android Studio test

    recorder Instrumentation tests
  39. None
  40. None
  41. Continuous Integration Can I perform unit testing automatically whenever software

    builds?
  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
  43. Demo Time MVP Pattern & Unit Testing

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

  45. No Questions!

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