Share
Embed
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
Transcript
88 slides
-
Kotlin Nullability Language Features Sealed Classes HOF Lambdas Contracts Inlining
Data Classes … Static Tools -
Kotlin Nullability Language Features Sealed Classes HOF Lambdas Contracts Inlining
Data Classes … Static Tools Lint Koshry Detekt … -
Kotlin Nullability Language Features Sealed Classes HOF Lambdas Contracts Inlining
Data Classes … Static Tools Lint Koshry Detekt … Testing -
@Test public void testThreeReturnsFizz() { final FizzBuzz fizzBuzz = new
FizzBuzz(); assertTrue(“Fizz", fizzBuzz.get(3)); } -
@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)) } -
@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)) } -
@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)) } -
@Test fun testThreeReturnsFizz() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" ==
fizzBuzz.get(3)) } @Test fun `three returns Fizz`() { val fizzBuzz = FizzBuzz() assertTrue("Fizz" == fizzBuzz.get(3)) } -
@Test public void testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(getMockListSectorDomain())); //
Act getDaily.getSectorByType("1"); // Assert verify(detailsSectorRepository).getSectorPerformanceData("1"); } -
@Test public void testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(getMockListSectorDomain())); //
Act getDaily.getSectorByType("1"); // Assert verify(detailsSectorRepository).getSectorPerformanceData("1"); } -
@Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act
getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") } -
@Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act
getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") } -
@Test fun testUseCaseGetSectorPerformanceDataCallsRepository() { // Arrange when(detailsSectorRepository.getSectorPerformanceData("1")) .thenReturn(Observable.just(emptyList())) // Act
getDaily.getSectorByType("1") // Assert verify(detailsSectorRepository).getSectorPerformanceData("1") } -
@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` -
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") } -
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") } -
val db = mock<SectorsDbInterface> { on { getDetailsBySector(anyString()) } doReturn
(Observable.just(getMockListSectorDb())) } -
val db = mock<SectorsDbInterface> { on { getDetailsBySector(anyString()) } doReturn
(Observable.just(getMockListSectorDb())) } -
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 ) } } -
@Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:
String): List<DetailSectorDb> -
@Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:
String): List<DetailSectorDb> -
@Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:
String): List<DetailSectorDb> val db = mock<SectorsDbInterface> { onBlocking { getDetailsBySector(anyString()) } doReturn getMockListSectorDb() } -
@Query("SELECT * FROM DetailSectorDb WHERE description= :description") suspend fun getDetailsBySector(description:
String): List<DetailSectorDb> val db = mock<SectorsDbInterface> { onBlocking { getDetailsBySector(anyString()) } doReturn getMockListSectorDb() } -
@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()) } } -
@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()) } } -
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 -
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 } }) -
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() } } } }) -
https://github.com/kotlintest/kotlintest Testing Styles Matchers/Assertions Inspectors Property Testing General Collections Types
Files Threads Dates clientIsConnected.shouldBeTrue() status.shouldBe("connected") -
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") -
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") } } }) -
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") } } }) -
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 -
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 } } -
https://github.com/kotlintest/kotlintest val test by tasks.getting(Test::class) { useJUnitPlatform { } }
testImplementation("io.kotlintest:kotlintest-runner-junit5:3.3.0") -
val description: KTextView = KTextView { withId(R.id.text_view_small) } val title:
KTextView = KTextView { withId(R.id.text_view_large) } -
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() } } } -
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() } } } -
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) } -
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) } -
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() }