Slide 1

Slide 1 text

Unit Testing in Android Anuj Middha Niharika Arora ADG Delhi Senior Software Engineer,1mg

Slide 2

Slide 2 text

Agenda ● Introduction to App Architecture and Unit Tests ● Tools and Frameworks ● Writing Testable Code ● Demo

Slide 3

Slide 3 text

Good/Clean Code Base? What is that?Why should we have good/clean code base?

Slide 4

Slide 4 text

Scalable | Stable | Testable | Modular

Slide 5

Slide 5 text

Application Architecture Why do I care?

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

MV* Patterns MV (C | P | VM) Responsible for displaying data View

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

MVC vs MVP vs MVVM Which one to follow?

Slide 12

Slide 12 text

Activity/Fragment/View should be Business logic free

Slide 13

Slide 13 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 14

Slide 14 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 15

Slide 15 text

Benefits of a unit test ▪ Find problems early. ▪ Facilitate refactoring. ▪ Simplify integration. ▪ Document code usage.

Slide 16

Slide 16 text

Types of Android unit test ▪ Instrumented unit test ▪ Local unit test

Slide 17

Slide 17 text

Instrumented unit test ▪ Runs on device or emulator ▪ Actually affects the device

Slide 18

Slide 18 text

Local unit test ▪ Runs on JVM ▪ No need for device or emulator ▪ Faster than instrumented unit test

Slide 19

Slide 19 text

Android Unit Testing Tools & Framework ● JUnit ● Mockito ● PowerMock ● Robolectric ● Espresso ● UI Automator

Slide 20

Slide 20 text

JUnit Gradle - testImplementation 'junit:junit:4.12'

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Mockito “ Objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

Slide 24

Slide 24 text

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" }

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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.

Slide 28

Slide 28 text

PowerMock testImplementation 'org.powermock:powermock-module-junit4:1.6.4' testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.4' testImplementation 'org.powermock:powermock-api-mockito:1.6.4' testImplementation 'org.powermock:powermock-classloading-xstream:1.6.4'

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

Writing Testable Code Or how not to lose your mind coding

Slide 32

Slide 32 text

Writing Tests can be hard!

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Testable Code

Slide 37

Slide 37 text

Deterministic

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Can you guess why this code is non deterministic?

Slide 42

Slide 42 text

Side Effects

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Solution?

Slide 45

Slide 45 text

Higher order functions

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 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 48

Slide 48 text

Red Flags ● Static properties and Fields

Slide 49

Slide 49 text

Red Flags ● Singletons

Slide 50

Slide 50 text

Red Flags ● Static Methods

Slide 51

Slide 51 text

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.

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

DEMO

Slide 54

Slide 54 text

References Github Link - https://github.com/Niharika8059/UnitTesting-MVVM-Kotlin-Coroutines- Sample Medium link - https://medium.com/1mgofficial/unit-testing-in-mvvm-kotlin-databinding -ba3d4ea08f0e

Slide 55

Slide 55 text

Resources https://semaphoreci.com/community/tutorials/stubbing-and-mocking-with-moc kito-2-and-junit https://android.jlelse.eu/better-testing-with-mvvm-ae74d4d872bd https://medium.com/mindorks/unit-testing-for-viewmodel-19f4d76b20d4

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

QUESTIONS?