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

State of Testing in Kotlin

State of Testing in Kotlin

We will check the different libraries and strategies to apply Kotlin testing in our projects.

Mallorca.

Iñaki Villar

April 03, 2019
Tweet

More Decks by Iñaki Villar

Other Decks in Technology

Transcript

  1. Kotlin Nullability Language Features Sealed Classes HOF Lambdas Contracts Inlining

    Data Classes … Static Tools Lint Koshry Detekt …
  2. Kotlin Nullability Language Features Sealed Classes HOF Lambdas Contracts Inlining

    Data Classes … Static Tools Lint Koshry Detekt … Testing
  3. @Test public void testThreeReturnsFizz() { final FizzBuzz fizzBuzz = new

    FizzBuzz(); assertTrue(“Fizz", fizzBuzz.get(3)); }
  4. @Test public void testThreeReturnsFizz() { final FizzBuzz fizzBuzz = new

    FizzBuzz(); assertTrue(“Fizz", fizzBuzz.get(3)); } @Test fun testThreeReturnsFizz() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" == fizzBuzz.get(3)) }
  5. @Test public void testThreeReturnsFizz() { final FizzBuzz fizzBuzz = new

    FizzBuzz(); assertTrue(“Fizz", fizzBuzz.get(3)); } @Test fun testThreeReturnsFizz() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" == fizzBuzz.get(3)) }
  6. @Test public void testThreeReturnsFizz() { final FizzBuzz fizzBuzz = new

    FizzBuzz(); assertTrue(“Fizz", fizzBuzz.get(3)); } @Test fun testThreeReturnsFizz() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" == fizzBuzz.get(3)) }
  7. @Test fun testThreeReturnsFizz() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" ==

    fizzBuzz.get(3)) } @Test fun `three returns Fizz`() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" == fizzBuzz.get(3)) }
  8. @Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange `when`(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act

    getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") } import org.mockito.Mockito.`when`
  9. val db = mock<SectorsDbInterface> { on { getDetailsBySector(anyString()) } doReturn

    (Observable.just(getMockListSectorDb())) } fun <R> on(methodCall: T.() -> R): OngoingStubbing<R> { return try { Mockito.`when`(mock.methodCall()) } catch (e: NullPointerException) { throw MockitoKotlinException( "", e ) } }
  10. @Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:

    String): List<DetailSectorDb> val db = mock<SectorsDbInterface> { onBlocking { getDetailsBySector(anyString()) } doReturn getMockListSectorDb() }
  11. @Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:

    String): List<DetailSectorDb> val db = mock<SectorsDbInterface> { onBlocking { getDetailsBySector(anyString()) } doReturn getMockListSectorDb() }
  12. @Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:

    String): List<DetailSectorDb> val db = mock<SectorsDbInterface> { onBlocking { getDetailsBySector(anyString()) } doReturn getMockListSectorDb() } fun <T : Any, R> KStubbing<T>.onBlocking( m: suspend T.() -> R ): OngoingStubbing<R> { return runBlocking { Mockito.`when`(mock.m()) } }
  13. @Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:

    String): List<DetailSectorDb> val db = mock<SectorsDbInterface> { onBlocking { getDetailsBySector(anyString()) } doReturn getMockListSectorDb() } fun <T : Any, R> KStubbing<T>.onBlocking( m: suspend T.() -> R ): OngoingStubbing<R> { return runBlocking { Mockito.`when`(mock.m()) } }
  14. https://github.com/kotlintest/kotlintest String Spec Fun Spec Should Spec Word Spec Feature

    Spec Behavior Spec Free Spec Testing Styles Matchers/Assertions Inspectors Property Testing
  15. https://github.com/kotlintest/kotlintest String Spec Fun Spec Should Spec Word Spec Feature

    Spec Behavior Spec Free Spec Testing Styles Matchers/Assertions Inspectors Property Testing class MyTests : StringSpec({ "strings.length should return size of string" { "hello".length shouldBe 5 } })
  16. https://github.com/kotlintest/kotlintest String Spec Fun Spec Should Spec Word Spec Feature

    Spec Behavior Spec Free Spec Testing Styles Matchers/Assertions Inspectors Property Testing class GetPairsImplTest : BehaviorSpec({ given("GetPairs Implementation") { `when`("There are no Pairs ") { val pairRepository = mock<PairsRepository> { } val getPairs = GetPairsImpl(pairRepository) whenever(pairRepository.getPairs()).thenReturn(emptyList()) getPairs.get() then("I should sync Pairs") { verify(pairRepository).syncPairs() } } } })
  17. https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing General Collections Types

    Files Threads Dates clientIsConnected.shouldBeTrue() status.shouldBe("connected") clientIsConnected shouldBe true status shouldBe("connected")
  18. https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing class TestInspectors :

    StringSpec({ "All the elements in the list formatted" { val xs = listOf("aa_1", "aa_2", "aa_3") xs.forAll { it.shouldContain("_") it.shouldStartWith("aa") } } })
  19. https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing class TestInspectors :

    StringSpec({ "All the elements in the list formatted" { val xs = listOf("aa_1", "aa_2", "aa_3") xs.forAll { it.shouldContain("_") it.shouldStartWith("aa") } } })
  20. https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing class TestInspectors :

    StringSpec({ "All the elements in the list formatted" { val xs = listOf("aa_1", "aa_2", "aa_3") xs.forAll { it.shouldContain("_") it.shouldStartWith("aa") } } }) forAll forNone forAtMostOne forAtLeastOne forAny
  21. https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing "String size" {

    assertAll { a: String, b: String -> (a + b).length shouldBe a.length + b.length } }
  22. val description: KTextView = KTextView { withId(R.id.text_view_small) } val title:

    KTextView = KTextView { withId(R.id.text_view_large) }
  23. val description: KTextView = KTextView { withId(R.id.text_view_small) } val title:

    KTextView = KTextView { withId(R.id.text_view_large) } @Test fun testContentScreen() { screen { content { isVisible() } description { click() isVisible() hasAnyText() } title { isVisible() hasAnyText() } } }
  24. val description: KTextView = KTextView { withId(R.id.text_view_small) } val title:

    KTextView = KTextView { withId(R.id.text_view_large) } @Test fun testContentScreen() { screen { content { isVisible() } description { click() isVisible() hasAnyText() } title { isVisible() hasAnyText() } } }
  25. val description: KTextView = KTextView { withId(R.id.text_view_small) } class KTextView

    : KBaseView<KTextView>, TextViewAssertions { constructor(function: ViewBuilder.() -> Unit) : super(function) constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function) }
  26. val description: KTextView = KTextView { withId(R.id.text_view_small) } class KTextView

    : KBaseView<KTextView>, TextViewAssertions { constructor(function: ViewBuilder.() -> Unit) : super(function) constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function) }
  27. val description: KTextView = KTextView { withId(R.id.text_view_small) } class KTextView

    : KBaseView<KTextView>, TextViewAssertions { constructor(function: ViewBuilder.() -> Unit) : super(function) constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function) } constructor(function: ViewBuilder.() -> Unit) { view = ViewBuilder().apply(function).getViewInteraction() }