I’m trying to clean a little bit my code and when testing my datasource I’m repeating a lot the test that returns connection and service error, and I’d like to have a bunch of code that tests everything, I’m using ParameterizedTest for other things and I thought this could be another way to do so, but I’m facing some problems like :
1.- I don’t have access to the classes I want the method of
2.- They are suspend fun so Arguments.arguments()
doesn’t accept that
Let’s say my codes are :
given(api.getFoo("1",true)).willReturn(
Response.error(
HttpURLConnection.HTTP_INTERNAL_ERROR,
"".toResponseBody("application/json".toMediaType()),
)
val actual = datasource.getFoo("1", true)
assertEquals(Result.failure<Throwable>(TechnicalError()),actual)
And then also the
given(otherApi.getBar("1",true)).willAnswer { throw IOException() }
val actual = datasource.getBar("1", true)
assertEquals(Result.failure<Throwable>(NetworkError()),actual)
So as you can see if I have like 4 api classes and 5 methods I need to repeat this x2 because all of the method can return TechincalError + NetworkError is there any way efficient and clean to do so?
I’ve also thought about make a list but I did not like the idea…