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

Droidcon London: Refactoring and Test Fakes

Ash Davies
October 27, 2023

Droidcon London: Refactoring and Test Fakes

Crafting resilient code is one of the most important things we do as software developers, but it's much easier said than done! Building with confidence requires an appropriate test harness and automated safeguards to ensure your software is robust.

In most real world scenarios, we don't have the luxury of working with a green field project, so it can be difficult to apply best practices whilst maintaining legacy code. How then can we refactor, and effectively utilise test fakes appropriately?

In this talk, I'll discuss the best approaches for using test fakes, mocks, stubs, and what are the pros and cons for each. How we can avoid writing brittle tests, slowing down development, and build scalable apps that can stand the test of time.

Ash Davies

October 27, 2023
Tweet

More Decks by Ash Davies

Other Decks in Programming

Transcript

  1. Legacy (adj.) Denoting or relating to so!ware or hardware that

    has been superseded but is di"cult to replace because of its wide use. ashdavies.dev
  2. Kotlin: Mutability fun sumAbsolute(list: MutableList<Int>): Int { for (i in

    list.indices) list[i] = abs(list[i]) return list.sum() } ashdavies.dev
  3. 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
  4. 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
  5. 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
  6. 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
  7. Testing: Dependencies Dependency Injection val heater = NuclearFusionHeater() // Expensive!

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

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

    prone! val maker = CoffeeMaker( pump = Thermosiphon(heater), heater = heater, ) assertTrue(maker.brew()) ashdavies.dev
  10. Test Doubles: Mocks val heater = mock<Heater>() val pump =

    mock<Pump>() val maker = CoffeeMaker( heater = heater, pump = pump, ) assertTrue(maker.brew()) ashdavies.dev
  11. Test Doubles: Mocks val heater = mock<Heater>() val pump =

    mock<Pump>() val maker = CoffeeMaker( heater = heater, pump = pump, ) assertTrue(maker.brew()) // ⚠ Fails! ashdavies.dev
  12. Test Doubles: Mocks val heater = mock<Heater> { on {

    isHeating } doAnswer { true } } val pump = mock<Pump> { on { pump() } doAnswer { true } } val maker = CoffeeMaker( heater = heater, pump = pump, ) assertTrue(maker.brew()) ashdavies.dev
  13. Test Doubles: Mocks Accidental Invocation spy(emptyList<String>()) { on { get(0)

    } doAnswer { "foo" } // throws IndexOutOfBoundsException } ashdavies.dev
  14. Test Doubles: Mocks API Sensitivity internal interface Heater { val

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

    fun <T : Any> heat(body: () -> T): T val isHeating: Boolean } val heater = mock<Heater> { on { isHeating } doAnswer { true } } ashdavies.dev
  16. 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
  17. Test Doubles: Mocks Pe!ormance Expensive real implementations replaced by expensive

    mocks. • Runtime code generation • Bytecode manipulation • Re!ection ! ashdavies.dev
  18. 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
  19. Test Doubles: Mocks Dynamic Mutability Framework generated mocks introduce a

    shared, mutable, dynamic, runtime declaration. ashdavies.dev
  20. You Don't Own Your Code! Your code belongs to your

    team. Be considerate. ashdavies.dev
  21. Test Doubles: Stubs Simple internal interface Pump { fun pump():

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

    pump(): Boolean } val stub = Pump { true } ashdavies.dev
  23. 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
  24. 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
  25. Testing: Fakes public class FakePump(private val onPump: (Boolean) -> Boolean)

    : Pump { public val pumped = mutableListOf<Pair<Boolean, Boolean>>() override fun pump(full: Boolean): Boolean = onPump(full).also { pumped += full to it } } ashdavies.dev
  26. Testing: Fakes Additional Behaviour class DelegatingHeater(private val delegate: Heater) :

    Heater by delegate { private val _drinks = mutableListOf<Any>() val drinks: List<Any> by ::_drinks override fun <T : Any> heat(body: () -> T): T { return delegate.heat(body).also { _drinks += it } } } ashdavies.dev
  27. Testing: Fakes Quali!cations Those who wrote the code are the

    most uniquely quali!ed to write the tests. ashdavies.dev
  28. Testing: In Memory internal fun interface CoffeeStore { fun has(type:

    CoffeeType): Boolean } internal enum class CoffeeType { CAPPUCCINO, ESPRESSO, LATTE, } ashdavies.dev
  29. Testing: In Memory internal class InMemoryCoffeeStore : CoffeeStore { private

    val _stock = mutableMapOf<CoffeeType, Int>() val stock: Map<CoffeeType, Int> 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
  30. 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
  31. 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