I got above error when i use mockhttpserver to test http request, the following code is how i create service.
   fun <T> getService(clazz: Class<T>): T {
        val client = OkHttpClient.Builder().addInterceptor(HeaderInterceptor()).addInterceptor(HttpLoggingInterceptor().apply { level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE })
                .build()
        return Retrofit.Builder()
                .baseUrl(mockWebServer.url(""))
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(GSON))
                .build()
                .create(clazz)
    }
This is my Test code.
@UninstallModules(HomePageDataModule::class)
@HiltAndroidTest
class TestHomePageViewModel {
    private lateinit var viewModel: HomePageViewModel
    @get:Rule
    var hiltRule = HiltAndroidRule(this)
    @Inject
    lateinit var cpd: CompositionDao
    @Inject
    lateinit var csd: CompositionsDao
    @Inject
    lateinit var hpds: HomePageDataSource
    @Inject
    lateinit var ss :HomePageService
    @Before
    fun init() {
        hiltRule.inject()
        viewModel = HomePageViewModel(HomeCompositionsRepository(cpd, csd, hpds, Util.GSON))
    }
    @Test
    fun testObserveHomeData() {
        val data = Util.getFileString("mainpage.json")
        val rr  = GSON.fromJson(data,Array<HomePreviewView>::class.java)
        println(rr)
        enqueueResponse("mainpage.json")
        runBlocking {
            val result = ss.getHomeData()
            Assert.assertNotEquals(rr.size,result.body()!!.size)
        }
}
Everything works smoothly on my app except running my unit test code. There is a similar problem , but my issue has a little difference compare to that one. Plenty of ways from that similar question i have tried, but not work.
PS: If the test code run on Junit4Test but not AndroidJunit4Test, it works properly. But now i need to exectue a integrate test. So this part of code need to be executed on AndroidJunit4Test
 
     
     
    