diff --git a/src/main/kotlin/concept/stc/coroutines/DispatchersProvider.kt b/src/main/kotlin/concept/stc/coroutines/DispatchersProvider.kt index 0bb195e..4118ff3 100644 --- a/src/main/kotlin/concept/stc/coroutines/DispatchersProvider.kt +++ b/src/main/kotlin/concept/stc/coroutines/DispatchersProvider.kt @@ -6,8 +6,7 @@ import org.springframework.stereotype.Component /** * Provides coroutines dispatchers. - * This class allows to replace background dispatchers with test dispatchers - * for unit and integration testing. + * This class allows to replace background dispatchers for unit and integration testing. */ @Component class DispatchersProvider { diff --git a/src/test/kotlin/concept/stc/data/ApiServiceIntegrationTest.kt b/src/test/kotlin/concept/stc/data/ApiServiceIntegrationTest.kt new file mode 100644 index 0000000..8999600 --- /dev/null +++ b/src/test/kotlin/concept/stc/data/ApiServiceIntegrationTest.kt @@ -0,0 +1,82 @@ +package concept.stc.data + +import com.ninjasquad.springmockk.MockkBean +import concept.stc.coroutines.DispatchersProvider +import concept.stc.data.local.MovieCrudRepository +import concept.stc.data.remote.ApiClient +import concept.stc.data.remote.model.SearchResponse +import io.mockk.coEvery +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.assertEquals + +@SpringBootTest(webEnvironment = WebEnvironment.NONE) +@AutoConfigureTestDatabase(replace = Replace.NONE) +class ApiServiceIntegrationTest { + + @OptIn(ExperimentalCoroutinesApi::class) + private val testDispatcher = UnconfinedTestDispatcher() + + @MockkBean + private lateinit var apiClient: ApiClient + + @MockkBean + private lateinit var dispatchers: DispatchersProvider + + @Autowired + private lateinit var repository: MovieCrudRepository + + @Autowired + private lateinit var service: ApiService + + @BeforeTest + fun setUp() { + coEvery { dispatchers.io } returns testDispatcher + } + + @AfterTest + fun cleanUp() { + runBlocking { repository.deleteAll() } + } + + @Test + fun `when load movies, given API response, then save them to database`() = runTest { + // Given + val movie = _movie.copy(imdbID = "testId") + val searchResponse = _searchResponse.copy(movies = listOf(movie)) + + coEvery { apiClient.search("test") } returns searchResponse + + // When + service.loadMovies("test") + + // Then + val saved = repository.findAll().first() + assertEquals("testId", saved.imdbID) + } + + private val _movie = SearchResponse.Movie( + title = "", + year = "", + imdbID = "", + type = "", + poster = "" + ) + + private val _searchResponse = SearchResponse( + movies = emptyList(), + totalResults = 0, + response = "" + ) +} diff --git a/src/test/kotlin/concept/stc/data/remote/ApiClientIntegrationTest.kt b/src/test/kotlin/concept/stc/data/remote/ApiClientIntegrationTest.kt index d892989..fa068f8 100644 --- a/src/test/kotlin/concept/stc/data/remote/ApiClientIntegrationTest.kt +++ b/src/test/kotlin/concept/stc/data/remote/ApiClientIntegrationTest.kt @@ -11,10 +11,11 @@ import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Value import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment import org.wiremock.spring.EnableWireMock import kotlin.test.assertEquals -@SpringBootTest +@SpringBootTest(webEnvironment = WebEnvironment.NONE) @EnableWireMock class ApiClientIntegrationTest {