【发布时间】:2017-10-13 05:42:33
【问题描述】:
我正在测试 API 端点。问题是当我检查 Swagger 时,它返回一个有效的令牌,但在我的测试中它返回 NULL。你能指出我在哪里做错了吗?
public static void createOrganization() {
Map<String, String> org = new HashMap();
org.put("directorName", "name");
org.put("email", "email@gmail.com");
org.put("website", "www.site.com");
org.put("phoneNumber", "000111");
String registrationToken = given().
contentType("application/json").
body(org).
when().
post("/v3/organizations").
then().
extract().path("registrationToken");
System.out.println("Token: "+ registrationToken);
输出:令牌:null
更新: 也许我错误地使用了 extract(),也许以后有不同的解决方案可以使用生成的值。因为对于 registerDevice() 我也得到了 NULL 。
public static void registerDevice(){
String clientDeviceId =
given().
param("phoneNumber", "000111").
param("model", "samsung").
param("platform", "0").
param("push_token",SenimEnvironmentVars.testPushToken).
param("uuid", SenimEnvironmentVars.testUUID).
param("version", "7.1").
when().
post("/v3/user-devices").
then().
contentType("application/json").
extract().path("clientDeviceId");
System.out.println("Device ID: "+ clientDeviceId); //also prints NULL
}
还有其他方法可以生成令牌、设备 ID 等并在以后使用它们吗?
【问题讨论】:
-
路径输入的值是否正确?'/'或需要正确的东西来制作路径?
-
请显示您的配置放心。服务器的主机、端口、基本路径等...
-
@VladislavKoroteev 因为其他测试正在运行,我不认为这是路径。但是这里是: public static void setup() { String basePathStr = System.getProperty("server.base"); if(basePathStr==null){ basePathStr = ""; } basePath = basePathStr;字符串 baseHost = System.getProperty("server.host"); if(baseHost==null){ baseHost = "localhost:8080"; } baseURI = baseHost; }
-
您是否尝试过记录输出以查看它返回的内容 -
then().log().body() -
在这种情况下,路径应该是
extract().path("content.registrationToken")
标签: java automated-tests rest-assured web-api-testing