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
- 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)
- 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
- 参数写在url里,不需要关心Header,可以用getForObject,如response是String:
new RestTemplate().getForObject(url + "?paramName1=paramValue1¶mName2=paramValue2", String.class)
- 如果关心Header,比如要放Cookie:
1>返回String:
HttpHeaders headers = new HttpHeaders()
headers.set(HttpHeaders.COOKIE, "")
HttpEntity request = new HttpEntity(headers)
new RestTemplate().exchange(url + "?paramName1=paramValue1¶mName2=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¶mName2=paramValue2", HttpMethod.GET, request, byte[].class)