【问题标题】:How to provide test retrofit url with Hilt如何使用 Hilt 提供测试改造 url
【发布时间】:2022-01-24 19:42:59
【问题描述】:

在我的应用程序中,我开始使用 Hilt 作为 DI。所以我创建了一个类来在我的存储库中提供改造,就像这样

@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)
    }

我的问题,如何更改 url 以在带有 Hilt 的 Mockwebserver 中使用它?

【问题讨论】:

    标签: android dependency-injection retrofit mockwebserver dagger-hilt


    【解决方案1】:

    将您的模块从 object 更改为 class 并创建 baseUrl 变量 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)
        }
    

    然后只需在您的测试源中创建一个新的测试模块:

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

    【讨论】:

      【解决方案2】:

      如果您使用不同的构建变体将模拟与真实分开,您可以在模拟和真实包中创建两个具有确切名称的类,并从该类中扩展RetrofitModule。然后将baseUrl等差异放在这两个类中。

      class RetrofitModuleConstants {
      
          val baseUrl = "https://my.fancy.api"
      }
      
      @InstallIn(ApplicationComponent::class)
      object RetrofitModule : RetrofitModuleConstants  {
          ...
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-01
        相关资源
        最近更新 更多