Last active
August 1, 2019 18:07
-
-
Save Audhil/761df96041c5cb0446b9ed171257f709 to your computer and use it in GitHub Desktop.
dagger module - https://proandroiddev.com/unit-tests-with-dagger2-retrofit2-rxjava2-and-mockk-android-1c268fa77957
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
@Module | |
open class ApplicationModule(private val application: GDelegate) { | |
@Provides | |
@Singleton | |
fun giveContext(): Context = this.application | |
@Provides | |
@Singleton | |
fun givePackageManager(): PackageManager = application.packageManager | |
@Provides | |
@Singleton | |
open fun giveLoggingInterceptor(): HttpLoggingInterceptor { | |
val interceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message -> | |
GLog.v("APP LOGG", message) | |
}) | |
interceptor.level = if (GLog.DEBUG_BOOL) | |
HttpLoggingInterceptor.Level.BODY | |
else | |
HttpLoggingInterceptor.Level.NONE | |
return interceptor | |
} | |
// okHttpClient | |
@Provides | |
open fun giveOkHttpClient( | |
loggingInterceptor: HttpLoggingInterceptor | |
): OkHttpClient { | |
val httpClient = OkHttpClient.Builder() | |
.addInterceptor(loggingInterceptor) | |
// for testing | |
// .addInterceptor { chain -> | |
// Response.Builder() | |
// .code(400) | |
// .message("{}") | |
// .request(chain.request()) | |
// .protocol(Protocol.HTTP_1_0) | |
// .body(ResponseBody.create(MediaType.parse("application/json"), "{}".toByteArray())) | |
// .build() | |
// } | |
// increasing time outs | |
.connectTimeout(1, TimeUnit.MINUTES) | |
.readTimeout(30, TimeUnit.SECONDS) | |
.writeTimeout(15, TimeUnit.SECONDS) | |
return httpClient.build() | |
} | |
@Provides | |
fun giveRetrofitBuilder(): Retrofit.Builder = | |
Retrofit.Builder() | |
.baseUrl(ConstantsUtil.API_ENDPOINT) | |
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
@Provides | |
open fun giveRetrofitService(okHttpClient: OkHttpClient): AppAPIs = | |
Retrofit.Builder() | |
.baseUrl(ConstantsUtil.API_ENDPOINT) | |
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.client(okHttpClient) | |
.build() | |
.create(AppAPIs::class.java) | |
@Provides | |
@Named("test") | |
open fun giveAppAPIs(): AppAPIs = | |
Retrofit.Builder().build().create(AppAPIs::class.java) | |
@Provides | |
@Singleton | |
fun giveGSONInstance(): Gson = Gson() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment