对于一个Web应用程序,我试图通过使用Java中的RestAssured框架编写api测试,并且该场景有2个步骤,如下所述;
我的第一个帖子请求在第一步中运行良好,但第二步却遇到422错误,正文响应给出了以下错误;
“错误”:[{“代码”:{“名称”:“ MemberNotFoundException”,“值”:“ MembershipService_M0002”},“消息”:“成员不存在。”}]}
可能是什么原因?
public class Assignment {
public String mobilenum;
static Random rand = new Random();
public static int number = rand.nextInt(900000)+100000;
@Test(priority=1, description="type customer informations for registration")
public void ApplyCredit() {
String name = Name();
String lastname = LastName();
String birthdate = BirthDate();
String DNIValue = GenerateDNI();
mobilenum = MobileNum();
System.out.println(name + " " + lastname+" "+birthdate+" "+ DNIValue + " " + mobilenum);
RestAssured.baseURI="https://test-v2-api.mykredit.com/api/customer/light";
//Request object
RequestSpecification httpRequest = RestAssured.given();
JSONObject requestparam = new JSONObject();
requestparam.put("acceptAgreement",true);
requestparam.put("acceptPolicy",true);
requestparam.put("birthdate", birthdate);
requestparam.put("countryCode", "+34");
requestparam.put("emailAddress", "blackkfredo2@gmail.com");
requestparam.put("firstName",name);
requestparam.put("gender", "male");
requestparam.put("lastName", lastname);
requestparam.put("mobilePhone", mobilenum);
requestparam.put("nationalID", DNIValue);
httpRequest.header("Content-Type","application/json; charset=utf-8");
httpRequest.body(requestparam.toJSONString());
Response response = httpRequest.request(Method.POST,"/register");
String responseBody = response.getBody().asString();
System.out.println(responseBody);
int statusLine = response.getStatusCode();
System.out.println("Status"+" "+statusLine);
Assert.assertEquals(statusLine, 200);
}
@Test(priority=2, description="type pin value for validation")
public void PinValidate() {
RestAssured.baseURI="https://test-v2-api.mykredit.com/api/customer/light";
RequestSpecification httpRequest2 = RestAssured.given();
JSONObject requestparam2 = new JSONObject();
System.out.println(mobilenum.substring(5));
requestparam2.put("pin",mobilenum.substring(5));
httpRequest2.header("Content-Type","application/json; charset=utf-8");
httpRequest2.body(requestparam2.toJSONString());
Response response = httpRequest2.request(Method.POST,"/pinValidate");
String responseBody = response.getBody().asString();
System.out.println(responseBody);
int status = response.getStatusCode();
System.out.println("Status"+" "+status);
Assert.assertEquals(status, 200);
}
public static String Name () {
String str = String.valueOf(number);
String otherString = "Name";
otherString=otherString+str;
return otherString;
}
public static String LastName () {
String str = String.valueOf(number);
String otherString = "LastName";
otherString=otherString+str;
return otherString;
}
public static String BirthDate() {
int randomdate = (int) (Math.random() * (2000 - 1940) + 1940);
String str = String.valueOf(randomdate);
int randommonth = (int) (Math.random() * (12 - 1) + 1);
String str2 = String.valueOf(randommonth);
int randomday = (int) (Math.random() * (30 - 1) + 1);
String str3 = String.valueOf(randomday);
String dateinfo = str+"-"+str2+"-"+str3;
return dateinfo;
}
public static String GenerateDNI() {
Random rand = new Random();
int dni = rand.nextInt(90000000)+100000;
String letters = "TRWAGMYFPDXBNJZSQVHLCKE";
char let = letters.charAt(dni % letters.length());
String dnivalue = "" + dni + let;
return dnivalue;
}
public static String MobileNum() {
Random rand = new Random();
int number = 10000000 + rand. nextInt(90000000);
String str = String.valueOf(number);
String firstdigit = "6";
firstdigit=firstdigit+str;
return firstdigit;
}
}