Share
Embed
Writing truly testable code 2.0 - Mobius Spb 2017
Many developers understand why testing is so important, but not everyone is writing tests on a daily basis. One of the most common issues is that it’s hard to test a codebase that was not designed to be testable. This talk will cover how to write code that can be tested easily. We will also talk about introducing testing to an existing not-test-ready codebase. While things like Android, MVP, Kotlin, and Rx will be used in examples, understanding of these technologies is not strictly required. Also, the approach itself is generic enough to be used on any platform or with any language.
Anton Rutkevich
April 21, 2017
Transcript
169 slides
-
Why tests? • Codebase grows -> chances of making a
mistake go up • Software gets released -> cost of a mistake goes up -> Fear of introducing modifications! -
The two major issues • Managing system complexity -> Rich
Hickey “Simple Made Easy” • Testing the system -> this talk -
What could possibly go wrong? System under test Network Platform
UI Image loading -
Agenda • Truly testable is ... • 3 rules of
truly testable code • Testability in practice • How to start? -
A non-pure function fun nextItemDescription(prefix: String): String { GLOBAL_VARIABLE++ return
"$prefix: $GLOBAL_VARIABLE" } -
A pure function fun itemDescription(prefix: String, itemIndex: Int): String {
return "$prefix: $itemIndex" } Pure function Inputs Outputs -
A function is easy to test if Arg[i] and R[i]
are explicit: passed through parameters and returned as results f(Arg[1], … , Arg[N]) -> (R[1], … , R[L]) -
Module inputs class Module( val title: String, // input )
{ fun doSomething() { // input // ... } } -
Module inputs class Module( val title: String, // input val
dependency: Explicit // dependency ) { fun doSomething() { // input val explicit = dependency.getCurrentState() // input // ... } } -
Module inputs class Module( val title: String, // input val
dependency: Explicit // dependency ) { fun doSomething() { // input val explicit = dependency.getCurrentState() // input val implicit = Implicit.getCurrentState() // input // ... } } -
Module inputs class Module( val title: String, // input val
dependency: Explicit // dependency ) { fun doSomething() { // input val explicit = dependency.getCurrentState() // input val implicit = Implicit.getCurrentState() // input // ... } } -
Module outputs class Module( ) { var state = "Some
state" fun doSomething() { state = "New state" // output // ... } } -
Module outputs class Module( val dependency: Explicit // dependency )
{ var state = "Some state" fun doSomething() { state = "New state" // output dependency.setCurrentState("New state") // output // ... } } -
Module outputs class Module( val dependency: Explicit // dependency )
{ var state = "Some state" fun doSomething() { state = "New state" // output dependency.setCurrentState("New state") // output Implicit.setCurrentState("New state") // output // ... } } -
Module outputs class Module( val dependency: Explicit // dependency )
{ var state = "Some state" fun doSomething() { state = "New state" // output dependency.setCurrentState("New state") // output Implicit.setCurrentState("New state") // output // ... } } -
Module inputs • Interactions with module API and dependencies API
• Values passed through the interactions • Order of the interactions • Timings between the interactions In[1], … , In[N] -
Module outputs • Interactions with module API and dependencies API
• Values passed through the interactions • Order of the interactions • Timings between the interactions • Modifications of the module state Out[1], … , Out[N] -
Testing a module Test = call of the function +
validation of the outputs M(In[1], … , In[N]) -> (Out[1], … , Out[L]) given, when then -
A module is easy to test if In[i] and Out[i]
are passed through module API or explicit dependencies API. M(In[1], … , In[N]) -> (Out[1], … , Out[L]) -
Explicit dependency test class Module(explicit: Explicit) { val tripled =
3 * explicit.getValue() } -
Explicit dependency test class Module(explicit: Explicit) { val tripled =
3 * explicit.getValue() } @Test fun testValueGetsTripled() { } -
Explicit dependency test class Module(explicit: Explicit) { val tripled =
3 * explicit.getValue() } @Test fun testValueGetsTripled() { // prepare Explicit dependency val result = Module( ??? ).tripled } -
Explicit dependency test class Module(explicit: Explicit) { val tripled =
3 * explicit.getValue() } @Test fun testValueGetsTripled() { // prepare Explicit dependency val result = Module( ??? ).tripled val expected = 15 } -
Explicit dependency test class Module(explicit: Explicit) { val tripled =
3 * explicit.getValue() } @Test fun testValueGetsTripled() { // prepare Explicit dependency val result = Module( ??? ).tripled val expected = 15 assertThat(result).isEqualTo(expected) } -
Explicit dependency test class Module(explicit: Explicit) { val tripled =
3 * explicit.getValue() } @Test fun testValueGetsTripled() { // prepare Explicit dependency val result = Module( ??? ).tripled val expected = 15 assertThat(result).isEqualTo(expected) } -
‘Explicit’ as a Singleton // ‘object’ stands for Singleton in
Kotlin object Explicit { fun getValue(): Int = ... } -
‘Explicit’ as a Singleton // ‘object’ stands for Singleton in
Kotlin object Explicit { fun getValue(): Int = ... } @Test fun testValueGetsTripled() { val result = Module( ??? ).tripled val expected = 15 assertThat(result).isEqualTo(expected) } -
‘Explicit’ as a final class // Classes are final by
default in Kotlin class Explicit { fun getValue(): Int = ... } -
‘Explicit’ as a final class // Classes are final by
default in Kotlin class Explicit { fun getValue(): Int = ... } @Test fun testValueGetsTripled() { val result = Module( ??? ).tripled val expected = 15 assertThat(result).isEqualTo(expected) } -
‘Explicit’ as an Interface interface Explicit { fun getValue(): Int
class Impl: Explicit { override fun getValue(): Int = ... } } -
‘Explicit’ as an Interface @Test fun testValueGetsTripled() { // prepare
Explicit dependency val result = Module( ??? ).tripled val expected = 15 assertThat(result).isEqualTo(expected) } -
‘Explicit’ as an Interface @Test fun testValueGetsTripled() { val mockedExplicit
= object : Explicit { override fun getValue(): Int = 5 } val result = Module(mockedExplicit).tripled val expected = 15 assertThat(result).isEqualTo(expected) } -
‘Explicit’ as an Interface @Test fun testValueGetsTripled() { val mockedExplicit
= object : Explicit { override fun getValue(): Int = 5 } val result = Module(mockedExplicit).tripled val expected = 15 assertThat(result).isEqualTo(expected) } -
Platform API 3rd party API Model Presenter UI Framework Platform
wrappers View MVP. Overview -
Platform API 3rd party API Model Presenter UI Framework Platform
wrappers View MVP. Overview -
Practical aspects 1. Understand explicits 2. Locate implicits and convert
them to explicits 3. Mock dependencies in tests 4. Abstract away the platform 5. Consider extracting implementation details -
Explicit inputs class ModuleInputs( input: String, inputLambda: () -> String,
inputObservable: Observable<String>, ) { } -
Explicit inputs class ModuleInputs( input: String, inputLambda: () -> String,
inputObservable: Observable<String>, ) { fun passInput(input: String) { } } -
Explicit inputs class ModuleInputs( input: String, inputLambda: () -> String,
inputObservable: Observable<String>, dependency: Explicit ) { private val someField = dependency.getInput() fun passInput(input: String) { } } -
Explicit inputs class ModuleInputs( input: String, inputLambda: () -> String,
inputObservable: Observable<String>, dependency: Explicit ) { private val someField = dependency.getInput() fun passInput(input: String) { } } -
Explicit outputs class ModuleOutputs( outputLambda: (String) -> Unit, ) {
fun getOutput(): String = "Output" init { outputLambda("Output") } } -
Explicit outputs class ModuleOutputs( outputLambda: (String) -> Unit, ) {
val outputObservable = Observable.just("Output") fun getOutput(): String = "Output" init { outputLambda("Output") } } -
Explicit outputs class ModuleOutputs( outputLambda: (String) -> Unit, dependency: Explicit
) { val outputObservable = Observable.just("Output") fun getOutput(): String = "Output" init { outputLambda("Output") dependency.passOutput("Output") } } -
Explicit outputs class ModuleOutputs( outputLambda: (String) -> Unit, dependency: Explicit
) { val outputObservable = Observable.just("Output") fun getOutput(): String = "Output" init { outputLambda("Output") dependency.passOutput("Output") } } -
#5: Statics and Singletons class Module { private val state
= Implicit.getCurrentState() } -
#5: Statics and Singletons class Module(dependency: Explicit) { private val
state = dependency.getCurrentState() } -
#5: Statics and Singletons class Module(dependency: Explicit) { private val
state = dependency.getCurrentState() } -
#4: Random generators class Module { private val fileName =
"some-file${Random().nextInt()}" } -
#4: Random generators class Module(rng: Rng) { private val fileName
= "some-file${rng.nextInt()}" } -
#4: Random generators class Module(rng: Rng) { private val fileName
= "some-file${rng.nextInt()}" } -
#3: File system & other storage class Module { fun
initStorage(path: String) { File(path).createNewFile() } } -
#3: File system & other storage class Module { fun
initStorage(path: String): FileCreationError? { return if (File(path).createNewFile()) { null } else { FileCreationError.Exists } } } -
#3: File system & other storage class Module { fun
initStorage(path: String): FileCreationError? = try { if (File(path).createNewFile()) { null } else { FileCreationError.Exists } } catch (e: SecurityException) { FileCreationError.Security(e) } catch (e: Exception) { FileCreationError.Other(e) } } -
#3: File system & other storage class Module { fun
initStorage(path: String): FileCreationError? = try { if (File(path).createNewFile()) { null } else { FileCreationError.Exists } } catch (e: SecurityException) { FileCreationError.Security(e) } catch (e: Exception) { FileCreationError.Other(e) } } -
#3: File system & other storage class Module(private val fileCreator:
FileCreator) { fun initStorage(path: String): FileCreationError? = try { if (fileCreator.createNewFile(path)) { null } else { FileCreationError.Exists } } catch (e: SecurityException) { FileCreationError.Security(e) } catch (e: Exception) { FileCreationError.Other(e) } } -
#3: File system & other storage class Module(private val fileCreator:
FileCreator) { fun initStorage(path: String): FileCreationError? = try { if (fileCreator.createNewFile(path)) { null } else { FileCreationError.Exists } } catch (e: SecurityException) { FileCreationError.Security(e) } catch (e: Exception) { FileCreationError.Other(e) } } -
#2: Time class Module { private val nowTime = System.currentTimeMillis()
private val nowDate = Date() // and all other time/date APIs } -
#2: Time class Module(time: TimeProvider) { private val nowTime =
time.nowMillis() private val nowDate = time.nowDate() // and all other time/date APIs } -
#2: Time class Module(time: TimeProvider) { private val nowTime =
time.nowMillis() private val nowDate = time.nowDate() // and all other time/date APIs } -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { } -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { val mobiusConfStart = 1492758000L } -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { val mobiusConfStart = 1492758000L val expected = "" val actual = MyTimePresenter(timestamp).formattedTimestamp assertThat(actual).isEqualTo(expected) } -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { val mobiusConfStart = 1492758000L val expected = "2017-04-21 10:00" val actual = MyTimePresenter(timestamp).formattedTimestamp assertThat(actual).isEqualTo(expected) } -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { val mobiusConfStart = 1492758000L val expected = "2017-04-21 10:00" val actual = MyTimePresenter(timestamp).formattedTimestamp assertThat(actual).isEqualTo(expected) } >> `actual on dev machine` = "2017-04-21 10:00" -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { val mobiusConfStart = 1492758000L val expected = "2017-04-21 10:00" val actual = MyTimePresenter(timestamp).formattedTimestamp assertThat(actual).isEqualTo(expected) } >> `actual on dev machine` = "2017-04-21 10:00" >> `actual on CI` = "2017-04-21 07:00" -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { val mobiusConfStart = 1492758000L val expected = "2017-04-21 10:00" val actual = MyTimePresenter(timestamp).formattedTimestamp assertThat(actual).isEqualTo(expected) } >> `actual on dev machine` = "2017-04-21 10:00" // UTC +3 >> `actual on CI` = "2017-04-21 07:00" // UTC -
#1: Formatting & Locales class MyTimePresenter(timestamp: Long) { val formattedTimestamp
= SimpleDateFormat("yyyy-MM-dd HH:mm") .format(timestamp) } fun test() { val mobiusConfStart = 1492758000L val expected = "2017-04-21 10:00" val actual = MyTimePresenter(timestamp).formattedTimestamp assertThat(actual).isEqualTo(expected) } >> `actual on dev machine` = "2017-04-21 10:00" // UTC +3 >> `actual on CI` = "2017-04-21 07:00" // UTC -
#1: Formatting & Locales Same for 1. NumberFormat 2. Currency
3. Locale 4. TimeZone 5. ... -
Interfaces still work everywhere interface MyService { fun doSomething() class
Impl(): MyService { override fun doSomething() { /* ... */ } } } -
Interfaces still work everywhere interface MyService { fun doSomething() class
Impl(): MyService { override fun doSomething() { /* ... */ } } } class TestService: MyService { override fun doSomething() { /* ... */ } } -
Interfaces still work everywhere interface MyService { fun doSomething() class
Impl(): MyService { override fun doSomething() { /* ... */ } } } class TestService: MyService { override fun doSomething() { /* ... */ } } val mockService = mock<MyService>() -
Interfaces still work everywhere interface MyService { fun doSomething() class
Impl(): MyService { override fun doSomething() { /* ... */ } } } class TestService: MyService { override fun doSomething() { /* ... */ } } val mockService = mock<MyService>() -
Extracting singletons object Implicit { fun getCurrentState(): String = "State"
} class SomeModule { init { val state = Implicit.getCurrentState() } } -
Extracting singletons interface StateProvider { fun getCurrentState(): String } object
Implicit { fun getCurrentState(): String = "State" } class SomeModule { init { val state = Implicit.getCurrentState() } } -
Extracting singletons interface StateProvider { fun getCurrentState(): String } object
Implicit: StateProvider { override fun getCurrentState(): String = "State" } class SomeModule { init { val state = Implicit.getCurrentState() } } -
Extracting singletons interface StateProvider { fun getCurrentState(): String } object
Implicit: StateProvider { override fun getCurrentState(): String = "State" } class SomeModule(stateProvider: StateProvider) { init { val state = stateProvider.getCurrentState() } } -
Extracting singletons interface StateProvider { fun getCurrentState(): String } object
Implicit: StateProvider { override fun getCurrentState(): String = "State" } class SomeModule(stateProvider: StateProvider) { init { val state = stateProvider.getCurrentState() } } -
Mockito 2 for mocking final PowerMock for mocking final, static,
singletons, … Other alternatives • Indicate design issues; can be avoided • Might blow up at some point -
Platform API 3rd party API Model Presenter UI Framework Platform
wrappers View MVP. View layer -
View layer approaches • Activity implements View • View as
a separate class -
Activity implements View interface SplashView { fun showLoading() } class
SplashActivity: Activity { override fun onCreate() { } } -
Activity implements View interface SplashView { fun showLoading() } class
SplashActivity: Activity, SplashView { override fun onCreate() { } override fun showLoading() { findViewById(R.id.progress).show() } } -
Activity implements View interface SplashView { fun showLoading() } class
SplashActivity: Activity, SplashView { override fun onCreate() { SplashPresenter(view = this) } override fun showLoading() { findViewById(R.id.progress).show() } } -
Activity implements View interface SplashView { fun showLoading() } class
SplashActivity: Activity, SplashView { override fun onCreate() { SplashPresenter(view = this) } override fun showLoading() { findViewById(R.id.progress).show() } } -
View as a separate class // Same as before class
SplashPresenter(view: SplashView) { init { view.showLoading() } } -
View as a separate class interface SplashView { fun showLoading()
class Impl : SplashView { override fun showLoading() { } } } -
View as a separate class interface SplashView { fun showLoading()
class Impl(private val viewRoot: View) : SplashView { override fun showLoading() { viewRoot.findViewById(R.id.progress).show() } } } -
View as a separate class interface SplashView { fun showLoading()
// Platform lives inside of Impl class Impl(private val viewRoot: View) : SplashView { override fun showLoading() { viewRoot.findViewById(R.id.progress).show() } } } -
View as a separate class class SplashActivity: Activity { override
fun onCreate() { } } -
View as a separate class class SplashActivity: Activity { override
fun onCreate() { // Platform View class val rootView: View = … } } -
View as a separate class class SplashActivity: Activity { override
fun onCreate() { // Platform View class val rootView: View = … SplashPresenter( view = SplashView.Impl(rootView) ) } } -
View as a separate class class SplashActivity: Activity { override
fun onCreate() { // Platform View class val rootView: View = … SplashPresenter( view = SplashView.Impl(rootView) ) } } -
Presenter is isolated from the platform @Test fun testLoadingIsShown() {
val mockedView = mock<SplashView>() } -
Presenter is isolated from the platform @Test fun testLoadingIsShown() {
val mockedView = mock<SplashView>() SplashPresenter(mockedView) } -
Presenter is isolated from the platform @Test fun testLoadingIsShown() {
val mockedView = mock<SplashView>() SplashPresenter(mockedView) verify(mockedView).showLoading() } -
Presenter is isolated from the platform @Test fun testLoadingIsShown() {
val mockedView = mock<SplashView>() SplashPresenter(mockedView) verify(mockedView).showLoading() } -
Platform API 3rd party API Model Presenter UI Framework Platform
wrappers View MVP. Platform wrappers -
They can exist in different forms • Static methods •
Singletons • Final classes • Non-final classes -
They can exist in different forms • Static methods •
Singletons • Final classes • Non-final classes -
They can exist in different forms • Static methods •
Singletons • Final classes • Non-final classes -
They can exist in different forms • Static methods •
Singletons • Final classes • Non-final classes -
They can exist in different forms • Static methods •
Singletons • Final classes • Non-final classes -
Creating a wrapper interface Wrapper { fun doSomething() class Impl:
Wrapper { override fun doSomething() { ThirdParty.doSomething() } } } -
Wrapper extra benefits • Fixes poor Platform API design •
Single Responsibility instead of God objects • Easy to apply 3rd party API changes -
Platform API 3rd party API Model Presenter UI Framework Platform
wrappers View MVP. Platform wrappers vs View -
Accessing string ids class SplashPresenter(view: SplashView, resources: Resources) { init
{ view.setTitle(resources.getString(R.string.welcome)) view.showLoading() } } -
Accessing string ids class SplashPresenter(view: SplashView, resources: Resources) { init
{ view.setTitle(resources.getString(R.string.welcome)) view.showLoading() } } interface Resources { fun getString(id: Int): String } -
Accessing string ids class SplashPresenter(view: SplashView, resources: Resources) { init
{ view.setTitle(resources.getString(R.string.welcome)) view.showLoading() } } -
Accessing string ids public final class R { public static
final class string { public static final int welcome=0x7f050000; } } -
Implementation details class SomeModule(input: String) { val state = calculateInitialState(input)
// Pure private fun calculateInitialState(input: String): String = "Some complex computation for $input" } -
Implementation details class SomeModule(input: String) { val state = calculateInitialState(input)
// Pure private fun calculateInitialState(input: String): String = "Some complex computation for $input" } -
Implementation details class SomeModule(input: String) { val state = calculateInitialState(input)
// Pure private fun calculateInitialState(input: String): String = "Some complex computation for $input" } class AnotherModule(input: String) { val state = calculateInitialState(input) // Pure private fun calculateInitialState(input: String): String = "Some complex computation for $input" } -
Implementation details class SomeModule(input: String) { val state = calculateInitialState(input)
// Pure private fun calculateInitialState(input: String): String = "Some complex computation for $input" } object StateCalculator { fun calculateInitialState(input: String): String = "Some complex computation for $input" } -
Implementation details class SomeModule(input: String) { val state = StateCalculator.calculateInitialState(input)
} class AnotherModule(input: String) { val state = StateCalculator.calculateInitialState(input) } object StateCalculator { fun calculateInitialState(input: String): String = "Some complex computation for $input" } -
Implementation details class SomeModule(input: String, stateCalculator: StateCalculator){ val state =
stateCalculator.calculateInitialState(input) } interface StateCalculator { fun calculateInitialState(input: String): String class Impl: StateCalculator { override fun calculateInitialState(input: String): String = "Some complex computation for $input" } } -
As a result • Implicit dependencies (singletons) become explicit (passed
through DI) • Initialization flow gets explicit • Models become easy to test -
Platform API 3rd party API Model Presenter UI Framework Platform
wrappers View MVP. Integration tests -
Platform API 3rd party API Model Presenter UI Framework Platform
wrappers View MVP. Integration tests -
3 rules • Pass arguments and return results explicitly •
Pass dependencies explicitly • Make sure you can mock dependencies -
Learning the art of programming, like most other disciplines, consists
of first learning the rules and then learning when to break them. Joshua Bloch, Effective Java