Skip to content
Advertisement

Using attributes of one class in another class

I am trying to move the assertThat method from Authentication class to the BDDStyledMethod class but the current code will generate the following error “‘Creds(java.lang.String)’ in ‘steps.Authentication’ cannot be applied to ‘()'”

How do i correct my code so that the assertThat method works in the BDDStyledMethod class ?

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"));
    }
}

Advertisement

Answer

The problem is with the Creds method. It is not returning anything and the exception is raised in this line -> Authentication.Creds().response.statusLine()
We can return a string from Creds method and then try to apply assert() on the returned string in GetActivityById class.

    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"));
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement