spring boot单元测试之RestTemplate(一)

写代码重要,写好的代码准确无误,且符合预期那更是必不可少。

spring boot内嵌了专门的单元测试模块——RestTemplate,保证了程序员可以对自己的代码进行及时的测试。

闲言少叙,直接上代码吧,简单的get/post方法都可以在这里测试,避免了自己写JDK原生的URLConnection、或Apache的Http Client方法。

一、什么是RestTemplate?

RestTemplate是Spring提供的用于访问Rest服务的客户端。

RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。

调用RestTemplate的默认构造函数,RestTemplate对象在底层通过使用java.net包下的实现创建HTTP 请求。

二、常用的方法:

1.getForEntity方法的返回值是一个ResponseEntity<T>ResponseEntity<T>是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。

2.getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject

3.postForEntity 和getForEntity类似

4.postForObject 如果只关注,返回的消息体,可以直接使用postForObject。用法和getForObject一致

5.postForLocation也是提交新资源,提交成功之后,返回新资源的URI,postForLocation的参数和前面两种的参数一致,只不过返回值为Uri,只需要服务提供者返回一个Uri即可,该Uri表示新资源的位置。

6.其他的还有put(),delete()都不怎么常用

三、代码

有一个特别的地方,post如果传参数只能用LinkedMultiValueMap

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class UrlOnlineTests {
    private RestTemplate template =new RestTemplate();
    @Test
    public void testGet(){
        try {
            String url = "http://localhost:8080/selectSmallVideo?sdt=20180531&edt=20180531";
            String result = template.getForObject(url, String.class);
            System.err.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
/**为什么这个地方只能用LinkedMutiValueMap*/
    @Test
    public void testPost(){
        try {
            String url = "http://localhost:8080/selectSmallVideo2";
            LinkedMultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
            map.add("sdt", 20180531);
            map.add("edt", 20180531);
            String result = template.postForObject(url,map, String.class);
            System.err.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGet2(){
        try {
            String url = "http://localhost:8080/selectSmallVideo?sdt=20180531&edt=20180531";
            ResponseEntity<String> entity = template.getForEntity(url, String.class);
            HttpStatus code = entity.getStatusCode();
            System.err.println(code);
            System.err.println(entity.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testExchange(){
        try {
            String url = "http://localhost:8080/selectSmallVideo?sdt=20180531&edt=20180531";
            HttpHeaders headers=new HttpHeaders();
            headers.add("devid","");
            headers.add("appver","9.3");
            HttpEntity<String> entity = new HttpEntity<>(null,headers);
            ResponseEntity<String> exchange = template.exchange(url, HttpMethod.GET, entity, String.class);
            System.err.println(exchange.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/wangbin2188/p/9182950.html