【问题标题】:REST ASSURED TEST creating my own given() methodREST ASSURED TEST 创建我自己的 given() 方法
【发布时间】:2018-08-24 07:32:36
【问题描述】:

我有两个以上类似的 REST ASSURED 休息,例如

 @Test
 public void makeSureThatGoogleIsUp() {
     given().auth().oauth2(getToken()).contentType("application/json")
     .when()
     .get("http://www.google.com")
     .then()
     .statusCode(200);
 }

 @Test
 public void makeSureThatGoogleIsUp() {
     given().auth().oauth2(getToken()).contentType("application/json")
     .when()
     .get("https://stackoverflow.com")
     .then()
     .statusCode(200);
 }

我想创建一个名为 given() 的方法,以降低方法的复杂性和可读性。

private [something] given(){
    return given().auth().oauth2(getToken()).contentType("application/json")
}

让我的方法使用我给定的而不是放心的方法:

 @Test
 public void makeSureThatGoogleIsUp() {
     given()
     .when()
     .get("http://www.google.com")
     .then()
     .statusCode(200);
 }

 @Test
 public void makeSureThatGoogleIsUp() {
     given()
     .when()
     .get("https://stackoverflow.com")
     .then()
     .statusCode(200);
 }

类似这里的东西:但我真的不知道这个方法可以有什么样的返回类型,或者它是否可能。 抱歉,问题可能很琐碎,但说真的,我被困在这里。 有什么帮助吗?谢谢! :)

【问题讨论】:

    标签: java spring testing java-8 rest-assured


    【解决方案1】:

    它返回一个RequestSpecification 对象:http://static.javadoc.io/com.jayway.restassured/rest-assured/2.4.1/com/jayway/restassured/specification/RequestSpecification.html

    另外,如果您要创建自己的 given(),最好将方法命名为其他名称,因为使用它的其他人可能会认为它是 jayway 版本,并在遇到错误时混淆自己。

    p>

    【讨论】:

    • 您在命名约定和返回类型方面是完全正确的。您的回答解决了我的问题,谢谢,祝您有愉快的一天:)
    • 很高兴知道它有帮助。请选择此作为正确答案,以便可以关闭线程。祝你有美好的一天! :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多