I'm trying to run integration tests with Ninja Framework (https://www.ninjaframework.org/documentation/testing_your_application/advanced.html). The service has a apiClient instance which interacts with 3rd party API using retrofit.
class Service
@Inject
constructor(
private val apiClient: ApiClient
)
I want to mock the response of apiClient.call. I have tried to set the apiClent annotated with Mock or initialize service with Service(apiClient) but it interacts with the actual API and returns a Timeout response.
@RunWith(NinjaRunner::class)
class IntegrationTest {
var apiClient: ApiClient = mock()
@Inject
var service: Service= mock()
@Test
fun `test something`() {
whenever(apiClient.call()).thenReturn(
RestResponse(status = RestResponse.Status.SUCCESS, message = "success")
)
val result = service.update()
}
}
Integration testing means checking if different modules are working fine when combined together as a group.* *Unit testing means testing individual modules of an application in isolation, to confirm that the code is doing things right.
Because you're testing Service with apiClient mocked what you probably need here are Unit tests.
You don't want to mock a class you're actually testing so one way here is to initialize Service with mocked objects and another to use @Mock annotation to create mocks at runtime. More on that here https://www.vogella.com/tutorials/Mockito/article.html