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

Unit Testing- What, Why and How?

Niharika Arora
September 05, 2020

Unit Testing- What, Why and How?

Slides covering few aspects of Unit Testing:
🔹 Why testing is important
🔹 What are the various testing types?
🔹 Tools and Frameworks for Unit Testing
🔹 Get familiar with testing annotations
🔹 How to write tests?
🔹 How to write testable code?

Session Link : https://www.youtube.com/watch?v=JC2s8iCoQm8&t=4897s&ab_channel=Recro

Niharika Arora

September 05, 2020
Tweet

More Decks by Niharika Arora

Other Decks in Technology

Transcript

  1. Unit Testing: What, Why and How?
    Niharika Arora
    Senior Software Engineer, 1mg

    View Slide

  2. Agenda
    ● Why Tests?

    View Slide

  3. Agenda
    ● Why Tests?
    ● Tools and Frameworks

    View Slide

  4. Agenda
    ● Why Tests?
    ● Tools and Frameworks
    ● Demo

    View Slide

  5. Agenda
    ● Why Tests?
    ● Tools and Frameworks
    ● Demo
    ● Writing Testable Code

    View Slide

  6. View Slide

  7. ● “Mobile apps are frontend apps, the real logic is in the backend, so
    backend apps should be tested instead.”

    View Slide

  8. ● “Mobile apps are frontend apps, the real logic is in the backend, so
    backend apps should be tested instead.”
    ● “Mobile apps are difficult to unit test, because most of the logic is done
    in the UI. At most, you should only care about UI tests.”

    View Slide

  9. ● “Mobile apps are frontend apps, the real logic is in the
    backend, so backend apps should be tested instead.”
    ● “Mobile apps are difficult to unit test, because most of the logic
    is done in the UI. At most, you should only care about UI tests.”
    ● “Mobile apps are “simple” or “tiny” compared to backend
    apps. Thus, effort should be put in the features instead of
    wasting time making tests.”

    View Slide

  10. Why test?

    View Slide

  11. View Slide

  12. Why test?
    ● No recurring cost of doing fixes when things break in
    production.

    View Slide

  13. Why test?
    ● No recurring cost of doing fixes when things break in
    production.
    ● Maintenance and Refactoring becomes easy.

    View Slide

  14. Why test?
    ● No recurring cost of doing fixes when things break in
    production.
    ● Maintenance and Refactoring becomes easy.
    ● Validating your changes.

    View Slide

  15. Why test?
    ● No recurring cost of doing fixes when things break in
    production.
    ● Maintenance and Refactoring becomes easy.
    ● Validating your changes.
    ● Debugging

    View Slide

  16. Why test?
    ● No recurring cost of doing fixes when things break in
    production.
    ● Maintenance and Refactoring becomes easy.
    ● Validating your changes.
    ● Debugging
    ● Simplify integration.

    View Slide

  17. Why test?
    ● No recurring cost of doing fixes when things break in
    production.
    ● Maintenance and Refactoring becomes easy.
    ● Validating your changes.
    ● Debugging
    ● Simplify integration.
    ● The list never ends.

    View Slide

  18. Testing Pyramid

    View Slide

  19. View Slide

  20. UI Tests

    View Slide

  21. UI Tests
    ● Interact with the UI of your app, they emulate the user behavior and
    assert UI results.

    View Slide

  22. UI Tests
    ● Interact with the UI of your app, they emulate the user behavior and
    assert UI results.
    ● Slowest and most expensive tests as they require a device/emulator to
    run.

    View Slide

  23. UI Tests
    ● Interact with the UI of your app, they emulate the user behavior and
    assert UI results.
    ● Slowest and most expensive tests as they require a device/emulator to
    run.
    ● Tools : Espresso and UI Automator

    View Slide

  24. Integration Tests

    View Slide

  25. Integration Tests
    ● When we need to check how our code interacts with other parts of the
    Android framework but without the complexity of the UI.

    View Slide

  26. Integration Tests
    ● How your code interacts with other parts of the Android framework
    but without the complexity of the UI.
    ● Don’t require a device/emulator to run

    View Slide

  27. Integration Tests
    ● How your code interacts with other parts of the Android framework
    but without the complexity of the UI.
    ● Don’t require a device/emulator to run
    ● Tools: Roboelectric.

    View Slide

  28. Unit Test

    View Slide

  29. 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).

    View Slide

  30. Unit Test
    ● Fastest and least expensive tests as they don’t require a
    device/emulator to run.

    View Slide

  31. Unit Test
    ● Fastest and least expensive tests as they don’t require a
    device/emulator to run.
    ● Testing one logical unit/component guarantee that our component
    works properly for the set of inputs that we expect.

    View Slide

  32. Unit Test
    ● Fastest and least expensive tests as they don’t require a
    device/emulator to run.
    ● Testing one logical unit/component guarantee that our component
    works properly for the set of inputs that we expect.
    ● Tools : JUnit and Mockito

    View Slide

  33. Good rule of Thumb

    View Slide

  34. Anatomy of a unit test

    View Slide

  35. Anatomy of a unit test
    ▪ Arrange all necessary preconditions and inputs.

    View Slide

  36. Anatomy of a unit test
    ▪ Arrange all necessary preconditions and inputs.
    ▪ Act on the object or method under test.

    View Slide

  37. 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.

    View Slide

  38. View Slide

  39. Android Unit Testing Tools &
    Frameworks

    View Slide

  40. Android Unit Testing Tools &
    Framework
    ● JUnit

    View Slide

  41. JUnit

    View Slide

  42. JUnit
    ● Testing a class/method that doesn't call Android APIs.

    View Slide

  43. JUnit
    ● Testing a class/method that doesn't call Android APIs.
    ● Assertion-based testing

    View Slide

  44. View Slide

  45. JUnit statement assertions
    assertFalse(condition)
    assertEquals(expected, actual, tolerance)
    assertNull(object)
    assertNotNull(object)
    assertSame(expected, actual)

    View Slide

  46. JUnit Annotations
    @Test
    @Before
    @After
    @BeforeClass
    @AfterClass
    @Ignore

    View Slide

  47. JUnit Limitations

    View Slide

  48. JUnit Limitations
    ● Your Object/class have external dependencies

    View Slide

  49. Android Unit Testing Tools &
    Framework
    ● JUnit
    ● Mockito

    View Slide

  50. Mockito
    “ A Java framework allowing the creation of test mock objects in
    automated unit tests .

    View Slide

  51. Mockito features
    ▪ Mocking

    View Slide

  52. @Mock
    private lateinit var loginModel: LoginModel

    View Slide

  53. Mockito features
    ▪ Mocking
    ▪ Stubbing

    View Slide

  54. `when`(UtilityClass.isNetworkConnected()).thenReturn(true)

    View Slide

  55. Mockito features
    ▪ Mocking
    ▪ Stubbing
    ▪ Argument matchers

    View Slide

  56. `when`(UtilityClass.isEmailValid(ArgumentMatchers.anyString())).thenReturn(true)

    View Slide

  57. Mockito features
    ▪ Mocking
    ▪ Stubbing
    ▪ Argument matchers
    ▪ Verifying number of invocations

    View Slide

  58. Mockito features
    ▪ Mocking
    ▪ Stubbing
    ▪ Argument matchers
    ▪ Verifying number of invocations
    ▪ Verifying order of invocations

    View Slide

  59. View Slide

  60. Mockito limitations

    View Slide

  61. Mockito limitations
    ▪ Cannot mock final classes

    View Slide

  62. Mockito limitations
    ▪ Cannot mock final classes
    ▪ Cannot mock static methods

    View Slide

  63. Mockito limitations
    ▪ Cannot mock final classes
    ▪ Cannot mock static methods
    ▪ Cannot mock final methods

    View Slide

  64. Mockito limitations
    ▪ Cannot mock final classes
    ▪ Cannot mock static methods
    ▪ Cannot mock final methods
    ▪ Cannot mock equals(), hashCode()

    View Slide

  65. Android Unit Testing Tools &
    Framework
    ● JUnit
    ● Mockito
    ● PowerMock

    View Slide

  66. PowerMock

    View Slide

  67. PowerMock
    ● Framework that extends other mock libraries such as
    Mockito with more powerful capabilities.

    View Slide

  68. PowerMock
    ● 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.

    View Slide

  69. Writing Testable Code
    Or how not to lose your mind coding

    View Slide

  70. Writing Tests can be hard!

    View Slide

  71. When done right, results in a clean, easy to maintain codebase

    View Slide

  72. Good Unit Test

    View Slide

  73. Good Unit Test
    ● Easy to write

    View Slide

  74. Good Unit Test
    ● Easy to write
    ● Readable

    View Slide

  75. Good Unit Test
    ● Easy to write
    ● Readable
    ● Reliable

    View Slide

  76. Good Unit Test
    ● Easy to write
    ● Readable
    ● Reliable
    ● Fast

    View Slide

  77. Good Unit Test
    ● Easy to write
    ● Readable
    ● Reliable
    ● Fast
    ● Truly Unit

    View Slide

  78. View Slide

  79. Testable Code

    View Slide

  80. Deterministic

    View Slide

  81. Deterministic

    View Slide

  82. View Slide

  83. View Slide

  84. Can you guess why this code is non deterministic?

    View Slide

  85. Side Effects

    View Slide

  86. Red Flags
    ● Static properties and Fields

    View Slide

  87. Red Flags
    ● Singletons

    View Slide

  88. Red Flags
    ● Static Methods

    View Slide

  89. Rule of Thumb

    View Slide

  90. 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

    View Slide

  91. “Writing a test is simple, but writing a code
    that can be tested is not so simple”

    View Slide

  92. References
    Github Link -
    https://github.com/niharika2810/UnitTesting-MVVM-Kotlin-Koin-Coroutines-Sa
    mple
    Medium link -
    https://thedroidlady.com/2019/07/10/unit-testing-in-mvvm-kotlin-databinding.ht
    ml

    View Slide

  93. Resources
    https://android.jlelse.eu/better-testing-with-mvvm-ae74d4d872bd
    https://blog.mindorks.com/mockito-cannot-mock-in-kotlin
    https://blog.mindorks.com/using-mockito-in-android-unit-testing-as-
    a-pro
    https://www.raywenderlich.com/195-android-unit-testing-with-mockit
    o
    https://medium.com/@mohitaunni/tdd-in-andoid-a-brief-story-part-1-
    22166d211750

    View Slide

  94. Let’s connect
    ● LinkedIn : https://www.linkedin.com/in/thedroidlady/
    ● Medium : https://medium.com/@nik.arora8059
    ● Github : https://github.com/niharika2810
    ● Twitter : https://twitter.com/theDroidLady
    ● Personal : https://thedroidlady.com/

    View Slide

  95. View Slide