27
Unit Testing in Kotlin Egor Andreevici Software Developer @ 1&1 @EgorAnd

Unit Testing in Kotlin

Embed Size (px)

Citation preview

Page 1: Unit Testing in Kotlin

Unit Testing in Kotlin

Egor AndreeviciSoftware Developer @ 1&1@EgorAnd

Page 2: Unit Testing in Kotlin

Android: In The Java Zone

Page 3: Unit Testing in Kotlin

Java Kotlin

+ Official+ Stable- Old- Verbose

+ Modern+ Expressive- Stable?- Support?

Page 4: Unit Testing in Kotlin

Starting with Kotlin on Android?

Start small!

Start with unit tests!

Page 5: Unit Testing in Kotlin

Unit Tests Are Good

Page 6: Unit Testing in Kotlin

Benefits of Unit Tests

Provide a safety net around your code

Document your code… what?

Promote good design practices

Page 7: Unit Testing in Kotlin

Bad Documentation

/*** Get a subview of the root view at a certain position.** @param position Position, 0 <= position < root child count* @return Subview at given position, or {@code null} if position is out of * bounds*/public View getViewAtPosition(int position) { if (position >= 0 && position < root.getChildCount()) { return root.getChildAt(position); } return null;}

Page 8: Unit Testing in Kotlin

Bad Documentation

/*** Get a subview of the root view at a certain position.** @param position Position, 0 <= position < root child count* @return Subview at given position, or {@code null} if position is out of* bounds*/public View getViewAtPosition(int position) { if (position >= 0 && position < root.getChildCount()) { return root.getChildAt(position); } throw new IndexOutOfBoundsException("Invalid position " + position);}

Page 9: Unit Testing in Kotlin

A Better Test

@Test public void shouldReturnNullIfCalledWithInvalidPosition() { doReturn(0).when(mockParent).getChildCount();

assertThat(docs.getViewAtPosition(0)).isNull();}

Page 10: Unit Testing in Kotlin

EvenBetterTest.kt

Page 11: Unit Testing in Kotlin

Kotlinize

@Test fun shouldReturnNullIfCalledWithInvalidPosition() { doReturn(0).`when`<ViewGroup>(mockParent).childCount

assertThat(docs.getViewAtPosition(0)).isNull()}

Page 12: Unit Testing in Kotlin

Better Method Names

@Test fun `Should return null if called with invalid position`() { doReturn(0).`when`<ViewGroup>(mockParent).childCount

assertThat(docs.getViewAtPosition(0)).isNull()}

Page 13: Unit Testing in Kotlin

Better Mocking with Mockito-Kotlin

● Reified types for mock creation

● Fluent method mocking syntax

● More niceties

Page 14: Unit Testing in Kotlin

Better Mocking with Mockito-Kotlin

private var mockParent = mock<ViewGroup>()

Page 15: Unit Testing in Kotlin

Better Mocking with Mockito-Kotlin

// private var mockParent = mock<ViewGroup>()

private var mockParent: ViewGroup = mock { on { childCount } doReturn 0}

Page 16: Unit Testing in Kotlin

Better Mocking with Mockito-Kotlin

@Test fun `Should return null if called with invalid position`() { whenever(mockParent.childCount) doReturn 0

assertThat(docs.getViewAtPosition(0)).isNull()}

Page 17: Unit Testing in Kotlin

How Does It Work?

inline fun <reified T : Any> mock(): T = Mockito.mock(T::class.java)

Page 18: Unit Testing in Kotlin

Better Assertions with Kluent

@Test fun `Should return null if called with invalid position`() { whenever(mockParent.childCount) doReturn 0

docs.getViewAtPosition(0) `should be` null}

Page 19: Unit Testing in Kotlin

How Does It Work?

import org.junit.Assert.*

infix fun Any?.`should be`(theOther: Any?) = assertSame(theOther, this)infix fun Any?.shouldBe(theOther: Any?) = this `should be` theOther

Page 20: Unit Testing in Kotlin

Other Goodies

Page 21: Unit Testing in Kotlin

Multiline Strings

val json = """ { "name": "Bob", "age": 57 } """

@Test fun `Should parse JSON correctly`() { gson.fromJson(json, Person::class.java) `should equal` Person("Bob", 57)}

Page 22: Unit Testing in Kotlin

“with” for Multiple Assertions

@Test fun `Should parse JSON correctly #2`() { val person = gson.fromJson(json, Person::class.java)

with(person) { name `should equal` "Bob" age `should equal` 57 }}

Page 23: Unit Testing in Kotlin

Factory Functions with Named Args

public class Person {

private String name; private int age; private boolean isAKotlinProgrammer;

public Person(String name, int age, boolean isAKotlinProgrammer) { this.name = name; this.age = age; this.isAKotlinProgrammer = isAKotlinProgrammer; }

…}

Page 24: Unit Testing in Kotlin

Factory Functions with Named Args

fun person( name: String = "", age: Int = 0, isAKotlinProgrammer: Boolean = false) =

Person(name, age, isAKotlinProgrammer)

@Test fun `Should ...`() { val person = person(name = "Bob", age = 57)

...}

Page 25: Unit Testing in Kotlin

@JvmOverloads

@JvmOverloadsfun person( name: String = "", age: Int = 0, isAKotlinProgrammer: Boolean = false) =

Person(name, age, isAKotlinProgrammer)

Page 26: Unit Testing in Kotlin

...and more magic in store!

Page 27: Unit Testing in Kotlin

Thanks!

Egor AndreeviciSoftware Developer @ 1&1@EgorAnd