Slide 1

Slide 1 text

State of Testing in otlin @inyaki_mwc

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

https://pusher.com/state-of-kotlin

Slide 8

Slide 8 text

https://insights.stackoverflow.com/survey/2018#most-loved-dreaded-and-wanted

Slide 9

Slide 9 text

https://pusher.com/state-of-kotlin https://insights.stackoverflow.com/survey/2018#most-loved-dreaded-and-wanted

Slide 10

Slide 10 text

*.dex *.class Java complier application.apk

Slide 11

Slide 11 text

*.dex *.class Java complier Kotlin complier application.apk

Slide 12

Slide 12 text

Kotlin JVM NATIVE JS Android Server IOS MacOs Linux Windows Web Assembly

Slide 13

Slide 13 text

Kotlin

Slide 14

Slide 14 text

Kotlin Nullability Language Features Sealed Classes HOF Lambdas Contracts Inlining Data Classes …

Slide 15

Slide 15 text

Kotlin Nullability Language Features Sealed Classes HOF Lambdas Contracts Inlining Data Classes … Static Tools

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

sourceSets { test { java.srcDirs += "src/test/kotlin" } }

Slide 19

Slide 19 text

sourceSets { test { java.srcDirs += "src/test/kotlin" } }

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

@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)) }

Slide 22

Slide 22 text

@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)) }

Slide 23

Slide 23 text

@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)) }

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

@Test fun testThreeReturnsFizz() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" == fizzBuzz.get(3)) } @Test fun `three returns Fizz`() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" == fizzBuzz.get(3)) }

Slide 26

Slide 26 text

@Test public void testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(getMockListSectorDomain())); // Act getDaily.getSectorByType("1"); // Assert verify(detailsSectorRepository).getSectorPerformanceData("1"); }

Slide 27

Slide 27 text

@Test public void testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(getMockListSectorDomain())); // Act getDaily.getSectorByType("1"); // Assert verify(detailsSectorRepository).getSectorPerformanceData("1"); }

Slide 28

Slide 28 text

@Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") }

Slide 29

Slide 29 text

@Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") }

Slide 30

Slide 30 text

@Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") }

Slide 31

Slide 31 text

@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`

Slide 32

Slide 32 text

https://github.com/nhaarman/mockito-kotlin

Slide 33

Slide 33 text

https://github.com/nhaarman/mockito-kotlin @Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange whenever(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") }

Slide 34

Slide 34 text

https://github.com/nhaarman/mockito-kotlin @Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange whenever(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") }

Slide 35

Slide 35 text

val db = mock { on { getDetailsBySector(anyString()) } doReturn (Observable.just(getMockListSectorDb())) }

Slide 36

Slide 36 text

val db = mock { on { getDetailsBySector(anyString()) } doReturn (Observable.just(getMockListSectorDb())) }

Slide 37

Slide 37 text

val db = mock { on { getDetailsBySector(anyString()) } doReturn (Observable.just(getMockListSectorDb())) } fun on(methodCall: T.() -> R): OngoingStubbing { return try { Mockito.`when`(mock.methodCall()) } catch (e: NullPointerException) { throw MockitoKotlinException( "", e ) } }

Slide 38

Slide 38 text

@Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description: String): List

Slide 39

Slide 39 text

@Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description: String): List

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

https://github.com/kotlintest/kotlintest

Slide 45

Slide 45 text

https://github.com/kotlintest/kotlintest

Slide 46

Slide 46 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing

Slide 47

Slide 47 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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 } })

Slide 50

Slide 50 text

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 { } val getPairs = GetPairsImpl(pairRepository) whenever(pairRepository.getPairs()).thenReturn(emptyList()) getPairs.get() then("I should sync Pairs") { verify(pairRepository).syncPairs() } } } })

Slide 51

Slide 51 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing

Slide 52

Slide 52 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing

Slide 53

Slide 53 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing General Collections Types Files Threads Dates

Slide 54

Slide 54 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing General Collections Types Files Threads Dates clientIsConnected.shouldBeTrue() status.shouldBe("connected")

Slide 55

Slide 55 text

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")

Slide 56

Slide 56 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing https://arrow-kt.io/

Slide 57

Slide 57 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing https://arrow-kt.io/

Slide 58

Slide 58 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing

Slide 59

Slide 59 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing

Slide 60

Slide 60 text

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") } } })

Slide 61

Slide 61 text

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") } } })

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing

Slide 64

Slide 64 text

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 } }

Slide 65

Slide 65 text

https://github.com/kotlintest/kotlintest testImplementation("io.kotlintest:kotlintest-runner-junit5:3.3.0")

Slide 66

Slide 66 text

https://github.com/kotlintest/kotlintest val test by tasks.getting(Test::class) { useJUnitPlatform { } } testImplementation("io.kotlintest:kotlintest-runner-junit5:3.3.0")

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

description.should(endWith(value))

Slide 69

Slide 69 text

description.should(endWith(value)) description should endWith(value)

Slide 70

Slide 70 text

description.should(endWith(value)) description should endWith(value) infix fun T.should(matcher: Matcher)

Slide 71

Slide 71 text

description.should(endWith(value)) description should endWith(value) infix fun T.should(matcher: Matcher)

Slide 72

Slide 72 text

description.should(endWith(value)) description should endWith(value) infix fun T.should(matcher: Matcher)

Slide 73

Slide 73 text

description.should(endWith(value)) description should endWith(value) infix fun T.should(matcher: Matcher)

Slide 74

Slide 74 text

DEMO

Slide 75

Slide 75 text

I am your Father Luke DEMO

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

onView(withId(R.id.overview_sectors_list)) .perform(click()) .check(matches(isDisplayed()))

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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() } } }

Slide 81

Slide 81 text

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() } } }

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

Spek MockK https://mockk.io/ Kluent https://markusamshove.github.io/Kluent/ https://spekframework.org/ https://www.kotlinresources.com/library/assertk/ AssertK

Slide 87

Slide 87 text

Strategies Testing Kotlin Mockito Kotlin Kotlin Test Kakao https://github.com/nhaarman/mockito-kotlin https://github.com/kotlintest/kotlintest https://github.com/agoda-com/Kakao

Slide 88

Slide 88 text

@inyaki_mwc Thanks