Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Agenda ● Why Tests?

Slide 3

Slide 3 text

Agenda ● Why Tests? ● Tools and Frameworks

Slide 4

Slide 4 text

Agenda ● Why Tests? ● Tools and Frameworks ● Demo

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

● “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.”

Slide 9

Slide 9 text

● “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.”

Slide 10

Slide 10 text

Why test?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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.

Slide 18

Slide 18 text

Testing Pyramid

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

UI Tests

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Integration Tests

Slide 25

Slide 25 text

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.

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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.

Slide 28

Slide 28 text

Unit Test

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Good rule of Thumb

Slide 34

Slide 34 text

Anatomy of a unit test

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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.

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Android Unit Testing Tools & Frameworks

Slide 40

Slide 40 text

Android Unit Testing Tools & Framework ● JUnit

Slide 41

Slide 41 text

JUnit

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

JUnit Limitations

Slide 48

Slide 48 text

JUnit Limitations ● Your Object/class have external dependencies

Slide 49

Slide 49 text

Android Unit Testing Tools & Framework ● JUnit ● Mockito

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

Mockito features ▪ Mocking

Slide 52

Slide 52 text

@Mock private lateinit var loginModel: LoginModel

Slide 53

Slide 53 text

Mockito features ▪ Mocking ▪ Stubbing

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

Mockito features ▪ Mocking ▪ Stubbing ▪ Argument matchers

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Mockito limitations

Slide 61

Slide 61 text

Mockito limitations ▪ Cannot mock final classes

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

PowerMock

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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.

Slide 69

Slide 69 text

Writing Testable Code Or how not to lose your mind coding

Slide 70

Slide 70 text

Writing Tests can be hard!

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

Good Unit Test

Slide 73

Slide 73 text

Good Unit Test ● Easy to write

Slide 74

Slide 74 text

Good Unit Test ● Easy to write ● Readable

Slide 75

Slide 75 text

Good Unit Test ● Easy to write ● Readable ● Reliable

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Testable Code

Slide 80

Slide 80 text

Deterministic

Slide 81

Slide 81 text

Deterministic

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Can you guess why this code is non deterministic?

Slide 85

Slide 85 text

Side Effects

Slide 86

Slide 86 text

Red Flags ● Static properties and Fields

Slide 87

Slide 87 text

Red Flags ● Singletons

Slide 88

Slide 88 text

Red Flags ● Static Methods

Slide 89

Slide 89 text

Rule of Thumb

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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/

Slide 95

Slide 95 text

No content