1、创建一个springboot项目,基于gradle的创建
1)new一个project
2)选择spring initializr
3)选择gradle project,然后next
4)选择一个web,然后next,然后finish
2.打开build.gradle,设置aliyun仓库,导入包
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
}
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
//指定阿里云镜像
maven{
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
mavenCentral()
}
dependencies {
compile 'biz.paluch.logging:logstash-gelf:1.6.0'
compile 'ch.qos.logback:logback-classic:1.1.3'
compile 'org.slf4j:log4j-over-slf4j:1.7.21'
compile 'com.alibaba:fastjson:1.2.28'
compile 'org.jodd:jodd-core:3.6.5'
compile 'org.jodd:jodd-http:3.6.5'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "org.spockframework:spock-core:1.0-groovy-2.4"
compile "org.spockframework:spock-spring:1.0-groovy-2.4"
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
3.在test/java目录下面创建一个groovy的目录
4.创建一个TestAll的.java文件,加入@RunWith(Suite.class)注解
@Suite.SuiteClasses({})注解,大括号中就填写groovy测试类的clazz,如代码:
package groovy;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestGateWay.class,
TestRawApiServer.class,
TestServerStatus.class
})
public class TestAll {
}
这里添加的类,在调用TestAll的时候将会全部的去执行
5.创建一个Testable文件,做为基类文件,里面编写get请求,post请求的方法,以及一些基础配置
我这个demo是:
使用post请求,将方法名和服务名做为请求条件,然后parameters作为参数进行的请求,示例中也有get请求,和一般的post请求
package groovy
import com.alibaba.fastjson.JSON
import jodd.http.HttpRequest
trait Testable {
def ip = "http://192.168.1.11"
def baseUrl = ip + ":8080/"
def fileUrl = ip + ":8084/"
def activitiUrl = ip + ":8083/"
def get(url, Map map) {
HttpRequest.get(baseUrl + url).query(map).send().bodyText()
}
def getValide(url, Map map) {
HttpRequest.get(fileUrl + url).query(map).send().bodyText()
}
def getAsMap(url, Map map) {
asMap(HttpRequest.get(baseUrl + url).query(map).send().bodyText())
}
def postAsMap(path, Map map) {
def bodytest = HttpRequest.post(baseUrl + path).form(map).send().bodyText()
asMap(bodytest)
}
def postBody(path, Map map, Map header) {
def body = HttpRequest.post(baseUrl + path).body(JSON.toJSONString(map))
header.each { k, v -> body.header(k, v) }
def bodytest = body.send().bodyText()
asMap(bodytest)
}
def postFile(path, File file, String name, String value) {
def res = HttpRequest.post(baseUrl + path).form("file", file).header(name, value).send().bodyText()
asMap(res)
}
def postAsList(url, Map map) {
JSON.parseObject(HttpRequest.post(baseUrl + url).form(map).send().bodyText(), List.class)
}
def asMap(def json) {
JSON.parseObject(json, Map.class)
}
def dispatchLoginRpc() {
def tokenResponse = postBody("/", [serviceName: "authService", methodName: "getToken"], ["GW_APP": "RPC"])
def token = tokenResponse["access_token"]
def path = "getValidCode/create.do"
getValide(path, [token: token])
def validCodeResponse = postBody('/', [serviceName: "authService", methodName: "getValidCode", parameters: [token: token]], [:])
def validCode = validCodeResponse["data"]
// when:
def response = postBody("/", [serviceName: "accountService", methodName: "login",
parameters : [username: "aaa", password: "aaa", validCode: validCode, deviceType: "android"]], ["GW_APP": "RPC", token: token])
// then:
// resonse["code"] == 0;
if(response["code"] == 0){
return token;
}else{
System.err.println("登录失败!")
}
}
def setBaseUrl(url) {
baseUrl = url
}
def getBaseUrl(){
baseUrl
}
def setFileUrl(url) {
fileUrl = url
}
def getFileUrl(){
fileUrl
}
def setIp(ipaddress) {
ip = ipaddress
}
def getIp(){
ip
}
}
6.创建TestLogin.groovy,这个可以每个请求都去执行登陆(每个case都去执行登陆)
TestLogin中执行登陆获取token
package groovy
import spock.lang.Specification
class TestLogin extends Specification implements Testable{
def token = dispatchLoginRpc();
}
7.编写测试代码,新创建一个TestGateWay.groovy
package groovy
class TestGateWay extends TestLogin implements Testable{
def getEmployee(){
when:
def response = postBody("/", [serviceName: "rosterService", methodName: "selectAllByPage"]
, ["GW_APP": "RPC", token: token])
then:
response["code"] == 0
}
}
8.直接执行即可实现测试
9.把以上的TestGateWay.class加入TestAll中的@Suite.SuiteClasses中,如上面例子
10.执行TestAll的run,即可测试所有的测试接口
github:
https://github.com/mx342/spockDemo
标签:map,请求,get,url,baseUrl,token,org,def,springboot From: https://blog.51cto.com/u_15932265/5993542