mercredi 22 mai 2019

Testing Repository with Retrofit

I am writing Unit Tests for Repository that makes calls using Retrofit2.

I wanted to test logic after receiving Call from server.

Problem: when running each test separately all of them pass, but when running all test for class only one is passing. The other tests fails, because of: Wanted but not invoked: ... However, there was exactly 1 interaction with this mock: which is referring to listener.onDataFailure(). The reason for that: Call always returns same object Calls.failure(IOException())as a result for each test.

Repository class code:

public class Repository {

    private static Repository repository;

    private Repository(@NonNull final RetrofitService service) {
        this.service = service;
    }

    public synchronized static Repository getInstance(RetrofitService service) {

        if (repository == null) {
            repository = new Repository(service);
        }
        return repository;
    }

    public void getDataFromApi(@NonNull final DataListener listener) {

            service.getData("application/json").clone().enqueue(new Callback<DataList>() {

                @Override
                public void onResponse(@NonNull Call<DataList> call, @NonNull Response<DataList> response) {

                    if (response.errorBody() == null && response.body() != null) {
                    listener.onDataSuccess(response.body())
                    } else {
                    listener.onDataFailure(new Exception(t.getMessage()));
                    }
                }

                @Override
                public void onFailure(@NonNull Call<DataList> call, @NonNull Throwable t) {

                    listener.onDataFailure(new Exception(t.getMessage()));

                }
            });
    }
}

RepositoryTest class code:


import com.google.common.truth.Truth.assertThat
import okhttp3.ResponseBody
import org.junit.*
import org.mockito.*
import org.mockito.Mockito.*
import retrofit2.Call
import retrofit2.Response
import retrofit2.mock.Calls
import java.io.IOException
import org.mockito.Mockito.`when` as mockitoWhen

class RepositoryTest {

    @Mock
    lateinit var retrofitService: RetrofitService
    @Mock
    lateinit var dataListener: DataListener
    @Mock
    lateinit var body: ResponseBody


    private lateinit var repo: Repository

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        repo = Repository.getInstance(retrofitService)
    }

    @Test
    fun `geteData given_failure result_onDataFailure called`() {
        val call: Call<DataList> = Calls.failure(IOException())
        mockitoWhen(retrofitService.getData(any(), any()))
            .thenReturn(call.clone())

        repo.getDataFromApi(dataListener)

        verify(dataListener).onDataFailure(ArgumentMatchers.any())

    }

    @Test
    fun `geteData given_success result_onDataSuccess called`() {
        val call: Call<DataList> = Calls.response(Response.success(dataList))
        mockitoWhen(retrofitService.getData(any(), any()))
            .thenReturn(call.clone())

        repo.getDataFromApi(dataListener)

        verify(dataListener).onDataFailure(ArgumentMatchers.any())

    }
}



Question: Why each unit test return same Call object(which result that tests are failing)?

Aucun commentaire:

Enregistrer un commentaire