如何对Retrofit API调用进行单元测试? [英] How to unit test Retrofit api calls?

查看:250
本文介绍了如何对Retrofit API调用进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为每个可能的代码块集成单元测试用例. 但是我在添加通过改型进行的api调用的测试用例时遇到了问题.

I am trying to integrate Unit test cases for every chunk of code possible. But I am facing issues while adding test cases for api calls that are made through retrofit.

JUnit编译器从不执行 CallBack函数中的代码.

The JUnit compiler never executes the code in the CallBack functions.

还有另一种方法可以使所有api调用同步以进行测试,但这并不是在我的应用程序中的每种情况下都能实现的.

There is another option of making all the api calls Synchronous for testing purpose, but that's not possible for every case in my app.

我该如何解决?我必须以任何方式在api调用中添加测试用例.

How can I sort this out? I have to add test cases in the api calls by any means.

推荐答案

我使用Mockito,Robolectric和Hamcrest库测试Retrofit回调.

I test my Retrofit callbacks using Mockito, Robolectric and Hamcrest libraries.

首先,在模块的build.gradle中设置lib堆栈:

First of all, set up lib stack in your module's build.gradle:

dependencies {
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile "org.mockito:mockito-core:1.10.19"
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
}

在jour项目的全局build.gradle中,将以下行添加到buildscript依赖项:

In jour project's global build.gradle add following line to buildscript dependencies:

classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'

然后在Android Studio中进入构建变体"菜单(要快速找到它,请按Ctrl + Shift + A并进行搜索),然后将测试工件"选项切换为单元测试". Android Studio会将您的测试文件夹切换到"com.your.package(测试)"(而不是androidTest).

Then enter "Build Variants" menu in Android Studio (to quickly find it, hit Ctrl+Shift+A and search for it), and switch "Test Artifact" option to "Unit Tests". Android studio will switch your test folder to "com.your.package (test)" (instead of androidTest).

好的.设置完成,该写一些测试了!

Ok. Set-up is done, time to write some tests!

假设您有一些改造api调用,以检索需要放入某个适配器的对象列表,以用于RecyclerView等.我们想测试适配器是否在成功调用时被正确的项目填充. 为此,我们需要切换您的Retrofit接口实现,以用于通过模拟进行调用,并利用Mockito ArgumentCaptor类进行一些虚假响应.

Let's say you've got some retrofit api calls to retrieve a list of objects that need to be put into some adapter for a RecyclerView etc. We would like to test whether adapter gets filled with proper items on successful call. To do this, we'll need to switch your Retrofit interface implementation, that you use to make calls with a mock, and do some fake responses taking advantage of Mockito ArgumentCaptor class.

@Config(constants = BuildConfig.class, sdk = 21,
    manifest = "app/src/main/AndroidManifest.xml")
@RunWith(RobolectricGradleTestRunner.class)
public class RetrofitCallTest {

    private MainActivity mainActivity;

    @Mock
    private RetrofitApi mockRetrofitApiImpl;

    @Captor
    private ArgumentCaptor<Callback<List<YourObject>>> callbackArgumentCaptor;

    @Before
    public void setUp() {            
        MockitoAnnotations.initMocks(this);

        ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class);
        mainActivity = controller.get();

        // Then we need to swap the retrofit api impl. with a mock one
        // I usually store my Retrofit api impl as a static singleton in class RestClient, hence:
        RestClient.setApi(mockRetrofitApiImpl);

        controller.create();
    }

    @Test
    public void shouldFillAdapter() throws Exception {
        Mockito.verify(mockRetrofitApiImpl)
            .getYourObject(callbackArgumentCaptor.capture());

        int objectsQuantity = 10;
        List<YourObject> list = new ArrayList<YourObject>();
        for(int i = 0; i < objectsQuantity; ++i) {
            list.add(new YourObject());
        }

        callbackArgumentCaptor.getValue().success(list, null);

        YourAdapter yourAdapter = mainActivity.getAdapter(); // Obtain adapter
        // Simple test check if adapter has as many items as put into response
        assertThat(yourAdapter.getItemCount(), equalTo(objectsQuantity));
    }
}

通过右键单击测试类并单击运行继续测试.

Proceed with the test by right clicking the test class and hitting run.

就是这样.我强烈建议您使用Robolectric(带有robolectric gradle插件)和Mockito,这些库使整个android应用程序的测试变得更加容易. 我已经从下面的博客帖子.另外,请参考此答案.

And that's it. I strongly suggest using Robolectric (with robolectric gradle plugin) and Mockito, these libs make testing android apps whole lotta easier. I've learned this method from the following blog post. Also, refer to this answer.

更新:如果您将Retrofit与RxJava一起使用,请查看关于此的其他答案也是.

Update: If you're using Retrofit with RxJava, check out my other answer on that too.

这篇关于如何对Retrofit API调用进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆