【问题标题】:Testing with unirest- java使用 unirest-java 进行测试
【发布时间】:2016-12-12 09:02:48
【问题描述】:

目前,我正在使用 unirest java。这是一个示例网址。这里参数s是设备秘密,m是mac地址,d是设备idhttp://baseUrl.com?s=235&m=12:25:14:25&d=25

现在我想自动化不同的测试用例,例如:null mac, null 设备 ID,空设备秘密 不同类型的设备 ID .. 像所有 字符。,所有数字,混合字符 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) 成功场景:-

输入:http://baseUrl/device?s=b30b33&m=d3r34ret34t5r&d=8

输出Json:

{"level":"info","msg":"REG_DEV_01","meta":[]}

2) 场景:- REG_DEV_PRMS_1_00 ---错误 ----缺少强制参数 - 设备机密 ----缺少任何参数

3) 场景:- REG_DEV_PRMS_2_00 ----error ----Mandatory params are missing - Mac Address ------任何参数丢失

4) 场景:- REG_DEV_PRMS_3_00 ----错误 ----缺少强制参数 - 设备 ID -----缺少任何参数

5) 场景:- REG_DEV_EXISTS_00 ----error ----具有相同mac地址的设备已经存在----已经存在注册设备

6) 场景:- REG_DEV_1_00 ---错误 ----设备机密与提供的设备 ID 不匹配 ----相同

7) 场景 REG_DEV_01 info ----设备注册成功-----一样

【问题讨论】:

标签: java unirest


【解决方案1】:

这里是使用 Unirest 进行场景 1、2、3 和 4 的示例 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>

【讨论】:

  • 如果您没有将本地服务器设置为在 8080 处侦听并返回适当的结果,则这些测试将失败。
猜你喜欢
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 2015-06-23
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多