简体   繁体   中英

How to provide test retrofit url with Hilt

In my application I started to use Hilt as DI. So I create a class to provide retrofit in in my repository like this

@InstallIn(ApplicationComponent::class)
object RetrofitModule {

    var baseUrl = "https://my.fancy.api"


    @Singleton
    @Provides
    fun providesRetrofitClient(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(providesOkHttpClient())
            .build()
    }

    @Singleton
    @Provides
    fun providesOkHttpClient(): OkHttpClient {
        val okHttpClientBuilder = OkHttpClient.Builder()
        val loggingInterceptor = HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY
        }
        okHttpClientBuilder.addInterceptor(loggingInterceptor)
        return okHttpClientBuilder.build()
    }

    @Singleton
    @Provides
    fun providesJokeGeneratorService(retrofit: Retrofit): FancyApiService {
        return retrofit.create(FancyApiService::class.java)
    }

My question, how can I change the url to use it in the Mockwebserver with Hilt?

If you are using different build variants to separate mock from real, you can create two classes with exact name in both mock and real package and extent RetrofitModule from that class. Then put differences such as baseUrl and etc. in those two classes.

class RetrofitModuleConstants {

    val baseUrl = "https://my.fancy.api"
}

@InstallIn(ApplicationComponent::class)
object RetrofitModule : RetrofitModuleConstants  {
    ...
}

Change your module from object to class and make the baseUrl variable open :

@InstallIn(SingletonComponent::class)
open class RetrofitModule {

    open var baseUrl = "https://my.fancy.api"


    @Singleton
    @Provides
    fun providesRetrofitClient(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(providesOkHttpClient())
            .build()
    }

    @Singleton
    @Provides
    fun providesOkHttpClient(): OkHttpClient {
        val okHttpClientBuilder = OkHttpClient.Builder()
        val loggingInterceptor = HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY
        }
        okHttpClientBuilder.addInterceptor(loggingInterceptor)
        return okHttpClientBuilder.build()
    }

    @Singleton
    @Provides
    fun providesJokeGeneratorService(retrofit: Retrofit): FancyApiService {
        return retrofit.create(FancyApiService::class.java)
    }

Then simply create a new test module inside your test source:

@Module
@TestInstallIn(
    components = [SingletonComponent::class],
    replaces = [RetrofitModule::class]
)
class TestRetrofitModule : RetrofitModule() {
    override var baseUrl = "https://localhost:8000"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM