Saffik
Saffik

Reputation: 1003

RestAssured Java: How to get header user and pass from setup method

I have a class which has the following

package com.example.misc;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.authentication.PreemptiveBasicAuthScheme;
import org.junit.BeforeClass;

public class QueryEndpoint {

    @BeforeClass
    public static void setup() {
        RestAssured.port = 8010;
        PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
        authScheme.setUserName("username123");
        authScheme.setPassword("password123");
        RestAssured.authentication = authScheme;

        String basePath;
        basePath = "/api/version1/";
        RestAssured.basePath = basePath;

        String baseHost;
        baseHost = "http://localhost";
        RestAssured.baseURI = baseHost;
        }

    }

Then in another class, I have a test...

package com.example.tests;

import com.example.misc.QueryEndpoint;
import org.junit.Test;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class ApiTest extends QueryEndpoint{


    @Test
    public void verifyTopLevelURL() {
        given()
                .auth(). preemptive().basic("username", "password")// THIS LINE DON'T WORK, need to add here something?
                .contentType("application/json")
                .when().get("/123456789").then()
                .body("fruit",equalTo("123456789"))
                .body("fruit.apple",equalTo(37))
                .body("fruit.red",equalTo("apple"))
                .statusCode(200);
    }

My Question is: How do I use the header + user + pass set in the method setup() and call that to be used in my test verifyTopLevelURL.

Upvotes: 0

Views: 1439

Answers (1)

anurag0510
anurag0510

Reputation: 763

You can directly use static variable approach as you are inheriting ApiTest Class from QueryEndpoint Class. Here is the code snippet :

Your QueryEndpoint Class :

package com.example.misc;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.authentication.PreemptiveBasicAuthScheme;
import org.junit.BeforeClass;

public class QueryEndpoint {
    static String userName = "username123";
    static String password = "password123";
    @BeforeClass
    public static void setup() {
        RestAssured.port = 8010;
        PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
        authScheme.setUserName(userName);
        authScheme.setPassword(password);
        RestAssured.authentication = authScheme;

        String basePath;
        basePath = "/api/version1/";
        RestAssured.basePath = basePath;

        String baseHost;
        baseHost = "http://localhost";
        RestAssured.baseURI = baseHost;
        }

    }

Your ApiTest Class :

package com.example.tests;

import com.example.misc.QueryEndpoint;
import org.junit.Test;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class ApiTest extends QueryEndpoint{


    @Test
    public void verifyTopLevelURL() {
        given()
                .auth(). preemptive().basic(userName, password)
                .contentType("application/json")
                .when().get("/123456789").then()
                .body("fruit",equalTo("123456789"))
                .body("fruit.apple",equalTo(37))
                .body("fruit.red",equalTo("apple"))
                .statusCode(200);
    }

You can do same thing with headers too. Hope this helped.

Upvotes: 1

Related Questions