Created
October 28, 2020 07:09
-
-
Save Ikhiloya/690514dbbb15927207a6a47f7ff294fb to your computer and use it in GitHub Desktop.
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
@Test(expected = NotFoundException::class) | |
@Throws(Exception::class) | |
fun testIfCustomAnnotationIsPresent() { | |
val httpLoggingInterceptor = HttpLoggingInterceptor( | |
HttpLoggingInterceptor.Logger { message: String? -> Timber.i(message) } | |
) | |
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | |
val networkInterceptor = NetworkInterceptor() | |
val client: OkHttpClient = OkHttpClient.Builder() | |
.addInterceptor(httpLoggingInterceptor) | |
.addNetworkInterceptor(networkInterceptor) | |
.addInterceptor( | |
object : OfflineCacheInterceptor() { | |
@Throws(IOException::class) | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val request: Request = chain.request() | |
/* tes the request method */ | |
Assert.assertEquals("GET", request.method) | |
val url = request.url | |
/* Test the url */ | |
Assert.assertEquals( | |
BASE_URL + PAYMENT_TYPES, | |
url.toString() | |
) | |
/* get the Cacheable annotation from the request */ | |
val invocation = request.tag( | |
Invocation::class.java | |
) | |
Assert.assertNotNull(invocation) | |
val annotation: Cacheable? = | |
invocation!!.method().getAnnotation(Cacheable::class.java) | |
Assert.assertNotNull(annotation) | |
Assert.assertEquals("Cacheable", annotation!!.annotationClass.simpleName) | |
// The following just ends the test with an expected exception. | |
// You could construct a valid Response and return that instead | |
// Do not return chain.proceed(), because then your unit test may become | |
// subject to the whims of the network | |
throw NotFoundException() | |
} | |
}) | |
.build() | |
val retrofit: Retrofit = Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.client(client) | |
.build() | |
val paymentService: PaymentService = | |
retrofit.create(PaymentService::class.java) | |
paymentService.getPaymentTypes().execute() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment