-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
969e7d3
commit 466f081
Showing
5 changed files
with
108 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
app/src/test/java/clean/architecture/omdb/data/api/ApiServiceIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package clean.architecture.omdb.data.api | ||
|
||
import clean.architecture.data.api.ApiService | ||
import clean.architecture.data.api.config.ApiConfig | ||
import clean.architecture.data.api.model.SearchResponse | ||
import clean.architecture.data.api.model.SearchResponse.Movie | ||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.Moshi | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
import kotlinx.coroutines.test.runTest | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
import javax.inject.Inject | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
@Config(application = HiltTestApplication::class) | ||
@HiltAndroidTest | ||
class ApiServiceIntegrationTest { | ||
|
||
@get:Rule | ||
val mockWebServer = MockWebServer() | ||
|
||
@get:Rule | ||
val hiltRule = HiltAndroidRule(this) | ||
|
||
@Inject | ||
lateinit var apiService: ApiService | ||
|
||
@Inject | ||
lateinit var moshi: Moshi | ||
|
||
private lateinit var jsonAdapter: JsonAdapter<SearchResponse> | ||
|
||
@Before | ||
fun init() { | ||
ApiConfig.setBaseUrl(mockWebServer.url("/").toString()) | ||
hiltRule.inject() | ||
jsonAdapter = moshi.adapter(SearchResponse::class.java) | ||
} | ||
|
||
@Test | ||
fun `when searching movies, given search response, then return list of movies`() = runTest { | ||
// Given | ||
val movie = _movie.copy(title = "test") | ||
val search = _searchResponse.copy(movies = listOf(movie)) | ||
val json = jsonAdapter.toJson(search) | ||
val mockResponse = MockResponse() | ||
.setBody(json) | ||
.setResponseCode(200) | ||
.addHeader("Content-Type", "application/json") | ||
mockWebServer.enqueue(mockResponse) | ||
|
||
// When | ||
val result = apiService.search(apiKey = "testKey", title = "test") | ||
|
||
// Then | ||
assertEquals(listOf(movie), result.movies) | ||
} | ||
|
||
private val _movie = Movie( | ||
title = "", | ||
year = "", | ||
imdbID = "", | ||
type = "", | ||
poster = "" | ||
) | ||
|
||
private val _searchResponse = SearchResponse( | ||
movies = emptyList(), | ||
totalResults = "", | ||
response = "" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters