Skip to content

Commit

Permalink
Fix test delays
Browse files Browse the repository at this point in the history
Launch delays in another coroutine to avoid runTest time-skipping delays.
Add opt-in to coroutine api to avoid warnings for using runTest
  • Loading branch information
Conhan93 committed Sep 28, 2022
1 parent 07ddfd3 commit 2429f98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/test/kotlin/Model/Service/ContentServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Model.Service.ContentService.ContentServiceImpl
import Model.Storage.ILocalStorage
import TestHelper.Resource.LoadTestResource
import TestHelper.Resource.getTestResource
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -23,6 +24,7 @@ import java.net.http.HttpResponse
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@OptIn(ExperimentalCoroutinesApi::class)
class ContentServiceTest {

val testMonster = getTestResource(LoadTestResource.blackDragon) as Monster
Expand All @@ -37,6 +39,7 @@ class ContentServiceTest {
`when`(storage.getMonsterByName(Mockito.anyString())).thenReturn(null)
}

@Suppress("UNCHECKED_CAST")
fun mockClientReturnsBody(body : String) : HttpClient {
val mockClient = mock(HttpClient::class.java)

Expand All @@ -51,6 +54,7 @@ class ContentServiceTest {
return mockClient
}


@Test
fun `Monster request by name returns monster`() = runTest {
lateinit var responseBody : String
Expand Down
9 changes: 7 additions & 2 deletions src/test/kotlin/ViewModel/SearchViewModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import TestHelper.Resource.LoadTestResource
import TestHelper.Resource.getTestResource
import ViewModel.Search.SearchEvent
import ViewModel.Search.SearchViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -21,6 +24,7 @@ import org.mockito.kotlin.whenever
import kotlin.test.assertEquals
import kotlin.test.assertNull

@OptIn(ExperimentalCoroutinesApi::class)
class SearchViewModelTest {

private lateinit var contentService: ContentService
Expand Down Expand Up @@ -89,6 +93,7 @@ class SearchViewModelTest {

}


@Test
fun `OnSearch should fetch monster`() = runTest {
val testMonster = getTestResource(LoadTestResource.blackDragon) as Monster
Expand All @@ -101,7 +106,7 @@ class SearchViewModelTest {
assertEquals(testMonster, it.getOrNull())
} )
assertEquals(false, viewModel.isDropDownMenuExpanded)
delay(1) // let contentrequest finish
withContext(Dispatchers.Default) { delay(10) } // let contentrequest finish
assertEquals(false, viewModel.isSearching)
assert(viewModel.name.isEmpty())
}
Expand All @@ -125,7 +130,7 @@ class SearchViewModelTest {

viewModel.onEvent(SearchEvent.onSearch {})
// let value be found and set
delay(2)
withContext(Dispatchers.Default) {delay(10)}

assertEquals(testMonster, property.value)
}
Expand Down

0 comments on commit 2429f98

Please sign in to comment.