在API测试中发送发布请求时出现错误422

问题描述

对于一个Web应用程序,我试图通过使用Java中的RestAssured框架编写api测试,并且该场景有2个步骤,如下所述;

  1. 通过键入客户信息来注册客户。
  2. 按下注册按钮后,应在下一页输入手机号码和otp密码。另外,所关注的密码等于客户手机号码的后四位。

我的第一个帖子请求在第一步中运行良好,但第二步却遇到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 @R_360_4045@ions 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","[email protected]");
    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;

    }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)