Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Photo by Nubelson Fernandes on Unsplash

Slide 3

Slide 3 text

ashdavies.dev

Slide 4

Slide 4 text

ashdavies.dev

Slide 5

Slide 5 text

Unce!ainty ashdavies.dev

Slide 6

Slide 6 text

Coupling ! ashdavies.dev

Slide 7

Slide 7 text

Inheritance ashdavies.dev

Slide 8

Slide 8 text

Mutability Photo by Richard Horvath on Unsplash

Slide 9

Slide 9 text

Unpredictability ashdavies.dev

Slide 10

Slide 10 text

? ashdavies.dev

Slide 11

Slide 11 text

Extreme Programming ashdavies.dev

Slide 12

Slide 12 text

Pair Programming ashdavies.dev

Slide 13

Slide 13 text

Test Driven Development ashdavies.dev

Slide 14

Slide 14 text

O!ensive Programming < > ashdavies.dev

Slide 15

Slide 15 text

ashdavies.dev

Slide 16

Slide 16 text

Kotlin: Mutability ashdavies.dev

Slide 17

Slide 17 text

Kotlin: Mutability fun sumAbsolute(list: MutableList): Int { for (i in list.indices) list[i] = abs(list[i]) return list.sum() } ashdavies.dev

Slide 18

Slide 18 text

Kotlin: Mutability private val GROUNDHOG_DAY = TODO("java.util.Date()") fun startOfSpring(): java.util.Date = GROUNDHOG_DAY val partyDate = startOfSpring() partyDate.month = partyDate.month + 1 // Date is mutable ! ashdavies.dev

Slide 19

Slide 19 text

Kotlin: Mutability Shared Mutable State ashdavies.dev

Slide 20

Slide 20 text

Kotlin: Immutability Unidirectional Data Flow ashdavies.dev

Slide 21

Slide 21 text

Kotlin: Immutability Collections fun List.toMutableList(): MutableList fun Map.toMutableMap(): MutableMap fun Set.toMutableSet(): MutableSet ashdavies.dev

Slide 22

Slide 22 text

Kotlin: Immutability org.jetbrains.kotlinx:kotlinx-collections-immutable fun Iterable.toPersistentList(): PersistentList fun Iterable.toPersistentSet(): PersistentSet ashdavies.dev

Slide 23

Slide 23 text

Kotlin: Immutability IntelliJ IDEA ashdavies.dev

Slide 24

Slide 24 text

Kotlin: Immutability Final Concretions By default, Kotlin classes are !nal – they can't be inherited ashdavies.dev

Slide 25

Slide 25 text

Testing Photo by DiEGO MüLLER on Unsplash

Slide 26

Slide 26 text

Photos by Daniel Romero, Taylor Vick on Unsplash

Slide 27

Slide 27 text

Testing ashdavies.dev

Slide 28

Slide 28 text

Anti-Pa!erns and Code Smell ashdavies.dev

Slide 29

Slide 29 text

Refactoring ashdavies.dev

Slide 30

Slide 30 text

Seams Photo by Jackson David on Unsplash

Slide 31

Slide 31 text

Refactoring: Seams internal class FitFilter { private val parser: Parser = Parser.newInstance() } ashdavies.dev

Slide 32

Slide 32 text

Refactoring: Seams =================================================================== diff --git a/FitFilter.kt b/FitFilter.kt - internal class FitFilter { - private val parser: Parser = Parser.newInstance() - } - + internal fun interface FitFilter { + fun filter(input: String): String + } + + internal fun FitFilter(parser: Parser) = FitFilter { input -> + parser.parse(input) + } ashdavies.dev

Slide 33

Slide 33 text

Refactoring: Seams ashdavies.dev

Slide 34

Slide 34 text

Refactoring Dependency Injection ashdavies.dev

Slide 35

Slide 35 text

Refactoring Dependency Injection =================================================================== diff --git a/CoffeeMaker.kt b/CoffeeMaker.kt - internal class CoffeeMaker { - private val heater: Heater = ElectricHeater() - private val pump: Pump = Thermosiphon(heater) - } + internal class CoffeeMaker( + private val heater: Heater, + private val pump: Pump, + ) ashdavies.dev

Slide 36

Slide 36 text

Refactoring Dependency Injection =================================================================== diff --git a/CoffeeMaker.kt b/CoffeeMaker.kt + internal class CoffeeMaker( + private val heater: Heater, - private val thermosiphon: Thermosiphon, + private val pump: Pump, + ) + + internal interface Pump { + fun pump() + } + - internal class Thermosiphon { + internal class Thermosiphon : Pump { ashdavies.dev

Slide 37

Slide 37 text

Testing: Dependencies Dependency Injection internal class CoffeeMaker( private val heater: Heater, private val pump: Pump, ) ashdavies.dev

Slide 38

Slide 38 text

Testing: Dependencies Dependency Injection val heater = NuclearFusionHeater() // Expensive! val maker = CoffeeMaker( pump = Thermosiphon(heater), heater = heater, ) assertTrue(maker.brew()) ashdavies.dev

Slide 39

Slide 39 text

Testing: Dependencies Dependency Injection val heater = DiskCachedHeater() // Stateful! val maker = CoffeeMaker( pump = Thermosiphon(heater), heater = heater, ) assertTrue(maker.brew()) ashdavies.dev

Slide 40

Slide 40 text

Testing: Dependencies Dependency Injection val heater = UnbalancedHeater() // Error prone! val maker = CoffeeMaker( pump = Thermosiphon(heater), heater = heater, ) assertTrue(maker.brew()) ashdavies.dev

Slide 41

Slide 41 text

Testing: Dependencies Test Doubles ashdavies.dev

Slide 42

Slide 42 text

Test Doubles Mocks ashdavies.dev

Slide 43

Slide 43 text

Test Doubles: Mocks val heater = mock() val pump = mock() val maker = CoffeeMaker( heater = heater, pump = pump, ) assertTrue(maker.brew()) ashdavies.dev

Slide 44

Slide 44 text

Test Doubles: Mocks val heater = mock() val pump = mock() val maker = CoffeeMaker( heater = heater, pump = pump, ) assertTrue(maker.brew()) // ⚠ Fails! ashdavies.dev

Slide 45

Slide 45 text

Test Doubles: Mocks val heater = mock { on { isHeating } doAnswer { true } } val pump = mock { on { pump() } doAnswer { true } } val maker = CoffeeMaker( heater = heater, pump = pump, ) assertTrue(maker.brew()) ashdavies.dev

Slide 46

Slide 46 text

Test Doubles: Mocks Footguns ashdavies.dev

Slide 47

Slide 47 text

Test Doubles: Mocks Accidental Invocation spy(emptyList()) { on { get(0) } doAnswer { "foo" } // throws IndexOutOfBoundsException } ashdavies.dev

Slide 48

Slide 48 text

Test Doubles: Mocks API Sensitivity internal interface Heater { val isHeating: Boolean } val heater = mock { on { isHeating } doAnswer { true } } ashdavies.dev

Slide 49

Slide 49 text

Test Doubles: Mocks API Sensitivity internal interface Heater { + fun heat(body: () -> T): T val isHeating: Boolean } val heater = mock { on { isHeating } doAnswer { true } } ashdavies.dev

Slide 50

Slide 50 text

Test Doubles: Mocks Data Classes ashdavies.dev

Slide 51

Slide 51 text

Test Doubles: Mocks Data Classes Just Don't. ashdavies.dev

Slide 52

Slide 52 text

Test Doubles: Mocks Default Answers =================================================================== diff --git a/PumpTest.kt b/PumpTest.kt - val heater: Heater = mock() + val heater: Heater = mock(defaultAnswer = RETURNS_SMART_NULLS) + + val isHeating: Boolean = heater.isHeating // false ashdavies.dev

Slide 53

Slide 53 text

Test Doubles: Mocks Pe!ormance Expensive real implementations replaced by expensive mocks. • Runtime code generation • Bytecode manipulation • Re!ection ! ashdavies.dev

Slide 54

Slide 54 text

Test Doubles: Mocks Dynamic Mutability internal class CoffeeMakerTest { private lateinit var heater: Heater @Before fun setUp() { heater = mock { on { isHeating } doAnswer { true } } } @Test fun `should brew coffee`() { // heater already has state! } } ashdavies.dev

Slide 55

Slide 55 text

Test Doubles: Mocks Dynamic Mutability Framework generated mocks introduce a shared, mutable, dynamic, runtime declaration. ashdavies.dev

Slide 56

Slide 56 text

Unpredictability Costs ashdavies.dev

Slide 57

Slide 57 text

Unpredictability: Costs Learning Curve ashdavies.dev

Slide 58

Slide 58 text

Unpredictability: Costs Peer Review ashdavies.dev

Slide 59

Slide 59 text

Unpredictability: Costs Risk of Bugs ashdavies.dev

Slide 60

Slide 60 text

Unpredictability: Costs Slowed Feature Delivery ashdavies.dev

Slide 61

Slide 61 text

Unpredictability Victims • Junior developers • New team members • Future you ashdavies.dev

Slide 62

Slide 62 text

Test Doubles: Mocks Don't Mock Classes You Don't Own testing.googleblog.com/2020/07/testing-on-toilet-dont-mock-types-you.html ashdavies.dev

Slide 63

Slide 63 text

You Don't Own Your Code! Your code belongs to your team. Be considerate. ashdavies.dev

Slide 64

Slide 64 text

Test Doubles: Mocks Interaction Veri!cation ashdavies.dev

Slide 65

Slide 65 text

Test Doubles Mocks ashdavies.dev

Slide 66

Slide 66 text

Test Doubles: Stubs ashdavies.dev

Slide 67

Slide 67 text

Test Doubles: Stubs Simple internal interface Pump { fun pump(): Boolean } internal object StubPump : Pump { override fun pump(): Boolean = true } ashdavies.dev

Slide 68

Slide 68 text

Test Doubles: Stubs Idiomatic internal fun interface Pump { fun pump(): Boolean } val stub = Pump { true } ashdavies.dev

Slide 69

Slide 69 text

Testing: Stubs API Sensitive + private const val DEFAULT_AMOUNT = 250 // ml + - internal fun interface Pump { + internal interface Pump { - fun pump(): Boolean + fun pump(amount: Int = DEFAULT_AMOUNT): Boolean + } ashdavies.dev

Slide 70

Slide 70 text

Testing: Stubs API Sensitive private const val DEFAULT_AMOUNT = 250 // ml internal interface Pump { fun pump(amount: Int = DEFAULT_AMOUNT): Boolean } val stub = Pump { true } // Compilation failure... ashdavies.dev

Slide 71

Slide 71 text

Testing: Fakes ashdavies.dev

Slide 72

Slide 72 text

Testing: Fakes public class FakePump(private val onPump: (Boolean) -> Boolean) : Pump { public val pumped = mutableListOf>() override fun pump(full: Boolean): Boolean = onPump(full).also { pumped += full to it } } ashdavies.dev

Slide 73

Slide 73 text

Testing: Fakes Additional Behaviour class DelegatingHeater(private val delegate: Heater) : Heater by delegate { private val _drinks = mutableListOf() val drinks: List by ::_drinks override fun heat(body: () -> T): T { return delegate.heat(body).also { _drinks += it } } } ashdavies.dev

Slide 74

Slide 74 text

Testing: Fakes Authoring ashdavies.dev

Slide 75

Slide 75 text

Testing: Fakes Quali!cations Those who wrote the code are the most uniquely quali!ed to write the tests. ashdavies.dev

Slide 76

Slide 76 text

Testing: In Memory internal fun interface CoffeeStore { fun has(type: CoffeeType): Boolean } internal enum class CoffeeType { CAPPUCCINO, ESPRESSO, LATTE, } ashdavies.dev

Slide 77

Slide 77 text

Testing: In Memory internal class InMemoryCoffeeStore : CoffeeStore { private val _stock = mutableMapOf() val stock: Map by ::_stock override fun has(type: CoffeeType): Boolean { return (_stock[type] ?: 0) > 0 } fun add(type: CoffeeType, amount: Int = 1) { _stock[type] = (_stock[type] ?: 0) + amount } } ashdavies.dev

Slide 78

Slide 78 text

Be Be!er ashdavies.dev

Slide 79

Slide 79 text

Inte!ace Segregation No code should be forced to depend on methods it does not use. Image: dribbble.com/shots/3251806-Inte!ace-Segregation-Principle

Slide 80

Slide 80 text

Testing: Android Legacy Inheritance ashdavies.dev

Slide 81

Slide 81 text

android.content.Context ashdavies.dev

Slide 82

Slide 82 text

Recap Extreme Programming ashdavies.dev

Slide 83

Slide 83 text

Recap Shared Mutable State ashdavies.dev

Slide 84

Slide 84 text

Recap Test Doubles ashdavies.dev

Slide 85

Slide 85 text

Thanks! ashdavies.dev

Slide 86

Slide 86 text

Fu!her Reading • Ma!in Flower: Mocks Aren't Stubs ma!infowler.com/a!icles/mocksArentStubs.html • Ma!in Fowler: Practical Test Pyramid ma!infowler.com/a!icles/practical-test-pyramid.html • Images: Monkey User monkeyuser.com • Michael Feathers: Working E"ectively with Legacy Code ISBN: 978-0-13117-705-5 • Sam Edwards: Wrapping Mockito Mocks for Reusability handstandsam.com/2020/06/08/wrapping-mockito-mocks-for-reusability • Steve Freeman, Nat Pryce: Growing Object-Oriented So#ware, Guided by Tests ISBN: 978-0-32150-362-6 • Testing on the Toilet: Don't mock Types You Don't Own testing.googleblog.com/2020/07/testing-on-toilet-dont-mock-types-you.html • Testing on the Toilet: Know Your Test Doubles testing.googleblog.com/2013/07/testing-on-toilet-know-your-test-doubles.html • Marcello Galhardo: No Mocks Allowed marcellogalhardo.dev/posts/no-mocks-allowed ashdavies.dev