目前,我正在使用unirest java。这是一个Url示例。这里参数s是设备机密,m是mac地址,d是设备ID http://baseUrl.com?s=235&m=12:25:14:25&d=25
现在我想自动化不同的测试用例,例如:null mac,null 设备ID,null设备秘密不同类型的设备ID ..像所有 字符。,所有numbesrs,混合字符n数字特殊 字符集等
这是我的代码:
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
/**
* Created by naveen on 12/12/16.
*/
public class uniresttest {
public static void main(String args[]) throws Exception
{
final HttpResponse<String> response= Unirest.get("http://baseUrl.com?s=235&m=12:25:14:25&d=25").asString();
System.out.println(response.getBody());
}
}
1)成功案例: -
输出Json:
{ “水平”: “信息”, “msg” 中: “REG_DEV_01”, “元”:[]}
2)场景: - REG_DEV_PRMS_1_00 ---错误----强制参数丢失 - 设备密码----缺少任何参数
3)场景: - REG_DEV_PRMS_2_00 ----错误----缺少强制参数 - Mac地址------任何参数都缺失
4)场景: - REG_DEV_PRMS_3_00 ----错误----缺少强制参数 - 设备ID -----任何参数缺失
5)场景: - REG_DEV_EXISTS_00 ----错误----已存在相同mac地址的设备----已存在注册设备
6)Sceanrio: - REG_DEV_1_00 ---错误----设备机密不会对所提供的设备ID进行匹配----相同
7)Sceanrio REG_DEV_01 info ----设备注册成功-----相同
答案 0 :(得分:0)
以下是针对方案1,2,3和4使用Unirest的示例JUnit测试。
您可以针对方案5,6和7类似地编写测试。
import org.junit.Test;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
//http://localhost:8080?s=235&m=12:25:14:25&d=25
public class RestTestUsingUniRest {
@Test
public void checkTheSuccessScenario() throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://localhost:8080/device?s=30&m=12:12:25&d=8")
.header("accept", "application/json")
.header("Content-Type", "application/json")
.asJson();
org.junit.Assert.assertNotNull(jsonResponse);
org.junit.Assert.assertEquals("info", jsonResponse.getBody().getObject().get("level"));
org.junit.Assert.assertEquals("REG_DEV_01", jsonResponse.getBody().getObject().get("msg"));
}
@Test
public void deviceSecretIsMissing() throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://localhost:8080?m=12:25:14:25&d=25")
.header("accept", "application/json")
.header("Content-Type", "application/json")
.asJson();
org.junit.Assert.assertNotNull(jsonResponse);
org.junit.Assert.assertEquals("REG_DEV_PRMS_1_00", jsonResponse.getBody().getObject().get("msg"));
}
@Test
public void macAddressIsMissing() throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://localhost:8080/device?s=30&d=8")
.header("accept", "application/json")
.header("Content-Type", "application/json")
.asJson();
org.junit.Assert.assertNotNull(jsonResponse);
org.junit.Assert.assertEquals("REG_DEV_PRMS_2_00", jsonResponse.getBody().getObject().get("msg"));
}
@Test
public void deviceIdIsMissing() throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://localhost:8080?m=12:25:14:25")
.header("accept", "application/json")
.header("Content-Type", "application/json")
.asJson();
org.junit.Assert.assertNotNull(jsonResponse);
org.junit.Assert.assertEquals("REG_DEV_PRMS_3_00", jsonResponse.getBody().getObject().get("msg"));
}
}
希望你已经有了这个JAR。如果没有,您可以使用以下依赖项从Maven获取Unirest Java JAR。
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>