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

Unit Testing in Android

Unit Testing in Android

A talk on Unit Testing in Android Delhi Meetup held in Gojek, Gurugram covering up basic concepts of Unit Testing, annotations, frameworks and how to write testable code

Session Link : https://www.youtube.com/watch?v=0K4S3vkulGc&t=8s&ab_channel=ADG-Delhi

Niharika Arora

February 22, 2020
Tweet

More Decks by Niharika Arora

Other Decks in Technology

Transcript

  1. Agenda • Introduction to App Architecture and Unit Tests •

    Tools and Frameworks • Writing Testable Code • Demo
  2. MV* Patterns MV (C | P | VM) 1. Data

    source of the application 2. Network layer, database operations Model
  3. MV* Patterns MV (C | P | VM) Controller manipulates,

    edit, uses data model and show it to users via View. In Android, Activity/Fragments can act as both View and Controller Controller
  4. MV* Patterns MV (C | P | VM) Presenter is

    a simple java class that do not contain any UI components, it just manipulates data from model and display in on View. Presenter
  5. MV* Patterns MV (C | P | VM) They provide

    data and functionality to be used by views. They are what define the structure and behavior of the actual application you are building ** Many View can be mapped to one View-Model ViewModel
  6. Unit Test “ A software testing method by which individual

    units of code, are tested to determine whether they are fit for use. The smallest testable part of an application (Classes and Methods).
  7. Anatomy of a unit test ▪ Arrange all necessary preconditions

    and inputs. ▪ Act on the object or method under test. ▪ Assert that the expected results have occurred.
  8. Benefits of a unit test ▪ Find problems early. ▪

    Facilitate refactoring. ▪ Simplify integration. ▪ Document code usage.
  9. Local unit test ▪ Runs on JVM ▪ No need

    for device or emulator ▪ Faster than instrumented unit test
  10. Android Unit Testing Tools & Framework • JUnit • Mockito

    • PowerMock • Robolectric • Espresso • UI Automator
  11. Mocking reasons Reason - Your Object have external dependencies Mockito

    is a Java framework allowing the creation of test mock objects in automated unit tests dependencies { testImplementation "org.mockito:mockito-core:2.11.0" }
  12. Mockito features ▪ Mocking ▪ Stubbing ▪ Argument matchers ▪

    Verifying number of invocations ▪ Verifying order of invocations
  13. Mockito limitations ▪ Cannot mock final classes ▪ Cannot mock

    static methods ▪ Cannot mock final methods ▪ Cannot mock equals(), hashCode()
  14. PowerMock PowerMock is a framework that extends other mock libraries

    such as Mockito with more powerful capabilities. Enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.
  15. Robolectric Unit Testing framework which allows Android application to be

    tested on JVM without an emulator or device. Robolectric provides implementation of Android SDKs by rewriting Android core libraries using shadow classes
  16. Robolectric Gradle - testImplementation “org.robolectric:robolectric:latestVersion” Robolectric handles inflation of views,

    resource loading, and lots of other stuff that’s implemented in native C code on Android devices. **Robolectric is not an integration test framework, i.e., you cannot not test the interaction of Android components with it.
  17. Good Unit Test • Easy to write • Readable •

    Reliable • Fast • Truly Unit
  18. Rule of Thumb • Write deterministic code • Minimize side

    effects • In essence, write pure functions Can impurity really be removed? • As much as possible, extract it out and keep it contained
  19. What is TDD Test-driven development (TDD) is an approach for

    software development where you write tests first, then use those tests to drive the design and development of your software application.
  20. TDD Cycle Red — think about what you want to

    develop Green — think about how to make your tests pass Refactor — think about how to improve your existing implementation
  21. “Writing a test is simple, but writing a code that

    can be tested is not so simple”