Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTLN-575 Testing Flow in Kotlin #1137

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core-kotlin-modules/core-kotlin-flows/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@
<version>3.4.11</version>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.6.0</version> <!-- Use the latest stable version -->
</dependency>

<!-- Kotlin Coroutines Test -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-test</artifactId>
<version>1.6.0</version> <!-- Use the latest stable version -->
<scope>test</scope>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pull these versions out into the properties block below, and we probably don't need these comments to use the latest version.

</dependency>

</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.baeldung.flowTest

import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class FlowTestUnitTest {

@Test
fun `simpleFlow should emit 1 2 3`() = runTest {
val flow = simpleFlow().toList()

assertEquals(listOf(1, 2, 3), flow)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting might be a little off here.


@Test
fun `transformedFlow should multiply values by 2`() = runTest {
val result = transformedFlow().toList()
assertEquals(listOf(2, 4, 6), result)
}

@Test
fun `errorFlow should throw Test Exception`() = runTest {
val flow = errorFlow()

val emittedValues = mutableListOf<Int>()
val exception = assertThrows<Exception> {
flow.collect { emittedValues.add(it) }
}

assertEquals(listOf(1, 2), emittedValues)
assertEquals("Test Exception", exception.message)

}

@Test
fun `cancellableFlow stops emitting after external cancellation`() = runTest {
val emittedValues = mutableListOf<Int>()
val job = launch {
cancellableFlow().collect { emittedValues.add(it) }
}

advanceTimeBy(2000)
job.cancelAndJoin()

assertEquals(listOf(0, 1), emittedValues)
}

@Test
fun `delayedFlow should handle delayed emissions`() = runTest {
val result = delayedFlow().toList()
assertEquals(listOf(1, 2), result)
}
}

fun simpleFlow(): Flow<Int> = flow {
emit(1)
emit(2)
emit(3)
}

fun transformedFlow(): Flow<Int> = flow {
emit(1)
emit(2)
emit(3)
}.map { it * 2 }

fun errorFlow(): Flow<Int> = flow {
emit(1)
emit(2)
throw Exception("Test Exception")
}

fun cancellableFlow(): Flow<Int> = flow {
repeat(5) {
emit(it)
delay(1000)
}
}

fun delayedFlow(): Flow<Int> = flow {
delay(500)
emit(1)
delay(500)
emit(2)
}