在android系统测试HTTP请求/响应 [英] Testing http request/response in android

查看:191
本文介绍了在android系统测试HTTP请求/响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我使用的是Sprint的休息模板制作API调用到Web服务器。但在测试项目中,我的测试方法用于String休息模板提出请求,我​​不想送真正的HTTP请求。​​

In my android application I'm using Sprint Rest Template for making API call to the webserver. But in test project where I test method for making requests with String ResT Template I don't want to send real HTTP requests.

有没有办法通过其他模板模拟发送HTTP请求?我可以提供我的preferred反应?

Is there any way to mock HTTP requests sent by rest template? Can I provide my preferred response?

推荐答案

是的,我在做类似如下:

Yes, I'm doing something like the following:

在您的build.gradle添加以下内容:

In your build.gradle add the following:

androidTestCompile("org.springframework:spring-test:3.2.8.RELEASE") {
    exclude module: "spring-core"
}

您想排除,以避免此异常

You want the exclusion to avoid this exception

java.lang.IllegalAccessError:类参考在pre-验证类解析
  意想不到的实施

java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

然后在您的测试做类似如下:

Then in your test do something like the following:

public void testService() throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        PeopleService peopleService = new PeopleService(restTemplate);

        MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
        mockServer.expect(requestTo("http://localhost:3000/api/v1-DEV/people"))
                .andExpect(method(HttpMethod.GET))
                .andExpect(header("Authorization", "Bearer TEST_TOKEN"))
                .andRespond(withSuccess("JSON DATA HERE", MediaType.APPLICATION_JSON));

        People people = peopleService.getPeople();

        mockServer.verify();

        assertThat(people).isNotNull();
        //Other assertions
}

下面是春天的例子(<一个href=\"http://docs.spring.io/spring/docs/3.2.7.RELEASE/javadoc-api/org/springframework/test/web/client/MockRestServiceServer.html\" rel=\"nofollow\">http://docs.spring.io/spring/docs/3.2.7.RELEASE/javadoc-api/org/springframework/test/web/client/MockRestServiceServer.html):

Here is an example from Spring (http://docs.spring.io/spring/docs/3.2.7.RELEASE/javadoc-api/org/springframework/test/web/client/MockRestServiceServer.html):

 RestTemplate restTemplate = new RestTemplate()
 MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);

 mockServer.expect(requestTo("/hotels/42")).andExpect(method(HttpMethod.GET))
     .andRespond(withSuccess("{ \"id\" : \"42\", \"name\" : \"Holiday Inn\"}", MediaType.APPLICATION_JSON));

 Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42);
 // Use the hotel instance...

 mockServer.verify();

另一种方法是使用的Mockito。包括在您的build.gradle以下内容:

Another way to do it is by using Mockito. Include the following in your build.gradle:

androidTestCompile "com.google.dexmaker:dexmaker:1.0"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.0"
androidTestCompile "org.mockito:mockito-core:1.9.5"

您将需要每个以上的正确使用的Mockito。

You'll need each of the above to use Mockito properly.

然后在你的测试做到以下几点:

Then in your test do the following:

public class TestClass extends InstrumentationTestCase {
    @Mock
    private RestTemplate restTemplate;

    protected void setUp() throws Exception {
        super.setUp();

        initMocks(this);
    }

    public void testWithMockRestTemplate() throws Exception {
        Hotel expectedHotel = new Hotel("Fake Hotel From Mocked Rest Template");
        when(restTemplate.getForObject("/hotels/{id}", Hotel.class, 42).thenReturn(expectedHotel);

        Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42);

        assertThat(hotel).isEqualTo(expectedHotel);

    }
}

希望这有助于!

这篇关于在android系统测试HTTP请求/响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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