Skip to content

Instantly share code, notes, and snippets.

@Ikhiloya
Created October 28, 2020 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ikhiloya/690514dbbb15927207a6a47f7ff294fb to your computer and use it in GitHub Desktop.
Save Ikhiloya/690514dbbb15927207a6a47f7ff294fb to your computer and use it in GitHub Desktop.
@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