model: don't unit test against the live server

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-12-17 12:06:14 +05:30
parent 76f613f82f
commit e504a2c591
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 22 additions and 2 deletions

View file

@ -15,6 +15,13 @@ import retrofit2.create
@Module @Module
@InstallIn(ActivityComponent::class) @InstallIn(ActivityComponent::class)
object ApiModule { object ApiModule {
@Provides
@BaseUrlQualifier
fun provideBaseUrl(): String {
return LobstersApi.BASE_URL
}
@Provides @Provides
fun provideClient(): OkHttpClient { fun provideClient(): OkHttpClient {
return OkHttpClient.Builder() return OkHttpClient.Builder()
@ -29,10 +36,11 @@ object ApiModule {
fun provideRetrofit( fun provideRetrofit(
client: Lazy<OkHttpClient>, client: Lazy<OkHttpClient>,
moshi: Lazy<Moshi>, moshi: Lazy<Moshi>,
@BaseUrlQualifier baseUrl: String
): Retrofit { ): Retrofit {
return Retrofit.Builder() return Retrofit.Builder()
.client(client.get()) .client(client.get())
.baseUrl(LobstersApi.BASE_URL) .baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create(moshi.get())) .addConverterFactory(MoshiConverterFactory.create(moshi.get()))
.build() .build()
} }

View file

@ -0,0 +1,11 @@
package dev.msfjarvis.lobsters.injection
import javax.inject.Qualifier
/**
* Qualifier for a string value that needs to be provided to the [ApiModule.provideRetrofit] method
* as the base URL of our API.
*/
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class BaseUrlQualifier

View file

@ -22,7 +22,8 @@ class LobstersApiTest {
private val okHttp = ApiModule.provideClient() private val okHttp = ApiModule.provideClient()
private val retrofit = ApiModule.provideRetrofit( private val retrofit = ApiModule.provideRetrofit(
{ okHttp }, { okHttp },
{ MoshiModule.provideMoshi() } { MoshiModule.provideMoshi() },
"http://localhost:8080/"
) )
private val apiClient = ApiModule.provideApi(retrofit) private val apiClient = ApiModule.provideApi(retrofit)