Skip to content

Commit

Permalink
Add integration test for ApiService
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonShapovalov committed Dec 29, 2024
1 parent 1cc2128 commit b1e65c0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
82 changes: 82 additions & 0 deletions src/test/kotlin/concept/stc/data/ApiServiceIntegrationTest.kt
Original file line number Diff line number Diff line change
@@ -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 = ""
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit b1e65c0

Please sign in to comment.