我正在嘗試將 assertThat 方法從 Authentication 類移動到 BDDStyledMethod 類,但當前代碼將生成以下錯誤“'steps.Authentication'中的'Creds(java.lang.String)'不能應用于'()'”
如何更正我的代碼以便 assertThat 方法在 BDDStyledMethod 類中作業?
public class Authentication {
public static void Creds(String url){
RequestSpecification httpRequest=RestAssured.given()
.auth().oauth2(Authentication.login("user","password"));
Response response = httpRequest.get(url);
ResponseBody body=response.getBody();
body.prettyPrint();
System.out.println("The status received: " response.statusLine());
assertThat("They are not the same",response.statusLine(),is("HTTP/1.1 200"));
}
}
public class BDDStyledMethod {
public static void GetActivityById(){
Authentication.Creds("www.randomurl.com");
assertThat("They are not the same",Authentication.Creds().response.statusLine(),is("HTTP/1.1 200"));
}
}
uj5u.com熱心網友回復:
問題在于 Creds 方法。它沒有回傳任何內容,并且在這一行中引發了例外 -> Authentication.Creds().response.statusLine()
我們可以從 Creds 方法回傳一個字串,然后嘗試在 GetActivityById 類中對回傳的字串應用 assert()。
public class Authentication {
public static String Creds(String url){
RequestSpecification httpRequest=RestAssured.given()
.auth().oauth2(Authentication.login("user","password"));
Response response = httpRequest.get(url);
ResponseBody body=response.getBody();
body.prettyPrint();
System.out.println("The status received: " response.statusLine());
return response.statusLine().toString();
}
}
public class BDDStyledMethod {
public static void GetActivityById(){
String returned_str = Authentication.Creds("www.randomurl.com");
assertThat("They are not the same",returned_str,is("HTTP/1.1 200"));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/496214.html