Groovy、Spock、功能测试ft、访问Http服务、RestTemplate

Groovy

Groovy可以运行在JVM上,可以直接使用Java类。可以def定义弱类型变量。默认public。https://baike.baidu.com/item/Groovy/180590?fr=aladdin
Groovy文档:http://cndoc.github.io/groovy-doc-cn/

Spock

Spock可以用于功能测试,也就是ft。
官网:https://spockframework.org/spock/docs/2.0/index.html
简单入门:
https://blog.csdn.net/qq_15060345/article/details/100756535
https://www.cnblogs.com/lovesqcc/p/8647201.html
优先了解setup()、cleanup()、setupSpec()、cleanupSpec();given、when、then、expect、where。

Http服务

访问Http服务,可以用JDK的HttpURLConnection、Apache的HttpClient、OKHttp、Spring的RestTemplate。Spring的RestTemplate可以使用前面三个Http工具对应的工厂,默认JDK的,可以切换成Apache或者OKHttp的,可以参考:https://blog.csdn.net/hanxiaotongtong/article/details/107755368
推荐使用OKHttp的Spring的RestTemplate。测试的host和port、多个数据库的连接信息,可以封装一层,写在配置文件里。

RestTemplate

get请求的headers中没有content-type这个字段,post 的 content-type 有。

post方式,需结合ContentType:
application/x-www-form-urlencoded:文本表单方式,不管value实际什么类型,都写String
multipart/form-data:value是Object,可用于文件上传
application/json:JSON方式
text/xml:XML方式

Post

  1. application/x-www-form-urlencoded方式:
		HttpHeaders headers = new HttpHeaders()
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED)
        headers.set(HttpHeaders.COOKIE, "")
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>()
        params.add("paramName", "paramValue")
        HttpEntity request = new HttpEntity(params, headers)
        new RestTemplate().postForObject(url, request, responseType)
  1. multipart/form-data方式:
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>()
params.add("paramName", paramValue)
File file = new File("Test")
if(file.exists()){
    FileUtils.writeByteArrayToFile(file, fileBytes)
}

params.add("fileSource", new FileSystemResource(file))
HttpHeaders headers = new HttpHeaders()
headers.setContentType(MediaType.MULTIPART_FORM_DATA)
HttpEntity request = new HttpEntity(params, headers)
new RestTemplate().postForObject(url, request, responseType)

3.JSON方式:

JSONObject params = new JSONObject().fluentPut("paramName", paramValue)
HttpHeaders headers = new HttpHeaders()
headers.setContentType(MediaType.APPLICATION_JSON)
HttpEntity request = new HttpEntity(params, headers)
new RestTemplate().postForObject(url, request, responseType)

Get

  1. 参数写在url里,不需要关心Header,可以用getForObject,如response是String:
new RestTemplate().getForObject(url + "?paramName1=paramValue1&paramName2=paramValue2", String.class)
  1. 如果关心Header,比如要放Cookie:
    1>返回String:
HttpHeaders headers = new HttpHeaders()
headers.set(HttpHeaders.COOKIE, "")
HttpEntity request = new HttpEntity(headers)
new RestTemplate().exchange(url + "?paramName1=paramValue1&paramName2=paramValue2", HttpMethod.GET, request, String.class)

2>返回ResponseEntity<byte[]>,如下载文件,返回byte[]时 :

HttpHeaders headers = new HttpHeaders()
headers.set(HttpHeaders.COOKIE, "")
HttpEntity request = new HttpEntity(headers)
new RestTemplate().exchange(url + "?paramName1=paramValue1&paramName2=paramValue2", HttpMethod.GET, request, byte[].class)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值