Unit Testing : Retrofit 2 Exception - API interfaces must not extend other interfaces

215 Views Asked by At

I am not extending any interface and all my interfaces are independent. While performing unit testing I am getting the following exception. During normal API calls, everything works fine.

 val retrofit: Retrofit = Retrofit.Builder()
            .baseUrl(url)
            .client(httpBuilder.build())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
        return retrofit.create(MyApi::class.java)

The exception is thrown at retrofit.create(..) only while performing the unit test. Is there a way to avoid this error?

1

There are 1 best solutions below

0
On

This can be fixed by using dependency injection for getting Service inside the method to be tested instead of creating the instance within the method.