带有改造的单元测试存储库

时间:2019-05-22 16:22:53

标签: java android unit-testing testing kotlin

我正在编写用于使用Retrofit2进行调用的存储库的单元测试。

从服务器收到Call后,我想测试逻辑。

问题:分别运行每个测试时,所有测试均通过,但是对类运行所有测试时,仅一个测试通过。其他测试失败,原因是:Wanted but not invoked: ... However, there was exactly 1 interaction with this mock:指向listener.onDataFailure()。这样做的原因:调用总是返回相同的对象Calls.failure(IOException())作为每次测试的结果。

存储库类代码:

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类代码:


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())

    }
}


因此,在上述1个测试通过的情况下,在同一对象上调用dataListener.onDataFailure的两种情况下,另一个测试均失败。 同时,分别运行这两种方法将获得2次通过。

问题:为什么每个单元测试都返回相同的Call对象(导致测试失败)?

0 个答案:

没有答案