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

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. • “Mobile apps are frontend apps, the real logic is

    in the backend, so backend apps should be tested instead.”
  2. • “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.”
  3. • “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.”
  4. Why test? • No recurring cost of doing fixes when

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

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

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

    things break in production. • Maintenance and Refactoring becomes easy. • Validating your changes. • Debugging • Simplify integration.
  8. 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.
  9. UI Tests • Interact with the UI of your app,

    they emulate the user behavior and assert UI results.
  10. 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.
  11. 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
  12. 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.
  13. 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
  14. 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.
  15. 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).
  16. Unit Test • Fastest and least expensive tests as they

    don’t require a device/emulator to run.
  17. 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.
  18. 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
  19. Anatomy of a unit test ▪ Arrange all necessary preconditions

    and inputs. ▪ Act on the object or method under test.
  20. 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.
  21. Mockito “ A Java framework allowing the creation of test

    mock objects in automated unit tests .
  22. Mockito features ▪ Mocking ▪ Stubbing ▪ Argument matchers ▪

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

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

    static methods ▪ Cannot mock final methods ▪ Cannot mock equals(), hashCode()
  25. 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.
  26. Good Unit Test • Easy to write • Readable •

    Reliable • Fast • Truly Unit
  27. 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
  28. “Writing a test is simple, but writing a code that

    can be tested is not so simple”
  29. 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/