java 正则表达式 引用_java配置引用变量以及正则表达式

第一步

打开 file-setting-Editor-Live Templates

dfa151236aaf

image.png

第二步,新建一个

点 右上角 + Tempiate Group

dfa151236aaf

image.png

第三步,自定义名称

dfa151236aaf

image.png

第四步,内部添加列表

点右上角+在 Live Template

dfa151236aaf

image.png

第五步,设置第一个变量名

dfa151236aaf

image.png

需要用到的语句

*

*

*

* @author chenmc

* @date $date$ $time$

* @param $params$

* @return $returns$

*/

第六步设置get 请求方法

dfa151236aaf

image.png

@Test

@Parameters({"host"})

public void $functionName$(String host){

System.out.println(host);

// API为接口地址,data为请求数据

String url = host + "$API$" + "?" +"$data$";

System.out.println(url);

//result 为响应结果

String result = HttpClientUtil.doGet(url);

System.out.println(result);

//assert 为预期结果

boolean actual = result.contains("$assert$");

Assert.assertEquals(actual, true);

}

第七步设置 post请求方法

dfa151236aaf

image.png

需要黏贴的语句

@Test

@Parameters({"host"})

public void $functionName$(String host) {

System.out.println(host);

// API为接口地址

String url = host + "$API$";

System.out.println(url);

//bodyData 请求参数

String bodyData = "$bodyData$";

//键值对 application/x-www-form-urlencoded

//json application/json

//xml application/xml

String contentType = "$contentType$";

String result = HttpClientUtil.doPost(url,contentType,bodyData);

System.out.println(result);

//assert 为预期结果

boolean actual = result.contains("$assert$");

Assert.assertEquals(actual, true);

}

第八步 正则提取式设置

dfa151236aaf

image.png

需要黏贴的语句

//正则表达式

String pattern = "$re1$";

Pattern r = Pattern.compile(pattern);

Matcher m = r.matcher(result);

String c = "";

if(m.find()){

String s = m.group(0);

System.out.println(s);

//正则表达式

String pattern1 = "$re2$";

Pattern r1 = Pattern.compile(pattern1);

Matcher m1 = r1.matcher(s);

if(m1.find()){

c = m1.group(0);

System.out.println(c);

}else{

System.out.println( s + " no matcher");

}

}else{

System.out.println(result + " no matcher");

}

例子

如果出现发红的地方 直接 alt+回车键即可补全

public class TestNEW {

public String csId;

@Test

@Parameters({"host"})

public void testUserSignUp(String host) {

System.out.println(host);

// API为接口地址

String url = host + "/user/signup";

System.out.println(url);

//bodyData 请求参数

String bodyData = "{\n"

+ " \"phone\": \"18105645537\",\n"

+ " \"pwd\": \"jia123456\",\n"

+ " \"rePwd\": \"jia123456\",\n"

+ " \"userName\": \"jia968\"\n"

+ "}";

//键值对 application/x-www-form-urlencoded

//json application/json

//xml application/xml

String contentType = "application/json";

String result = HttpClientUtil.doPost(url,contentType,bodyData);

System.out.println(result);

//正则表达式

String pattern = "\"cstId\":(.*?),";

Pattern r = Pattern.compile(pattern);

Matcher m = r.matcher(result);

String c = "";

if(m.find()){

String s = m.group(0);

System.out.println(s);

//正则表达式

String pattern1 = "\\d+";

Pattern r1 = Pattern.compile(pattern1);

Matcher m1 = r1.matcher(s);

if(m1.find()){

c = m1.group(0);

System.out.println(c);

}else{

System.out.println( s + " no matcher");

}

}else{

System.out.println(result + " no matcher");

}

csId =c;

//assert 为预期结果

boolean actual = result.contains("0000");

Assert.assertEquals(actual, true);

}

@Test

@Parameters({"host"})

public void testCstreaLname(String host) {

System.out.println(host);

// API为接口地址

String url = host + "/cst/realname";

System.out.println(url);

//bodyData 请求参数

String bodyData = "{\n"

+ " \"birthday\": \"2018-10-17T03:53:27.008Z\",\n"

+ " \"certno\": \"342423198105063389\",\n"

+ " \"city\": \"上海\",\n"

+ " \"cstId\":"+ csId+" ,\n"

+ " \"cstName\": \"贾铭\",\n"

+ " \"email\": \"6666666@qq.com\",\n"

+ " \"province\": \"未知\",\n"

+ " \"sex\": 1\n"

+ "}";

//键值对 application/x-www-form-urlencoded

//json application/json

//xml application/xml

String contentType = "application/json";

String result = HttpClientUtil.doPost(url,contentType,bodyData);

System.out.println(result);

//assert 为预期结果

boolean actual = result.contains("0000");

Assert.assertEquals(actual, true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值