0

Share Test Context in Cucumber,while creating object in java to share same state for all scenarios i am getting exception

here i am creating object of endpoints class from TestContext class

Failure Trace

java.lang.IllegalStateException: Cannot stop.  Current container state was: CONSTRUCTED
    at org.picocontainer.lifecycle.DefaultLifecycleState.stopping(DefaultLifecycleState.java:72)
    at org.picocontainer.DefaultPicoContainer.stop(DefaultPicoContainer.java:794)
    at io.cucumber.picocontainer.PicoFactory.stop(PicoFactory.java:35)
    at io.cucumber.core.runner.Runner.disposeBackendWorlds(Runner.java:173)
    at io.cucumber.core.runner.Runner.runPickle(Runner.java:69)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:149)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:83)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:24)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:185)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:83)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at io.cucumber.junit.Cucumber$RunCucumber.evaluate(Cucumber.java:219)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

LoginSteps.java

package stepDefinition;

import apiEngine.Endpoints;
import apiEngine.model.requests.AuthorizationRequest;
import apiEngine.model.responses.Token;
import cucumber.TestContext;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class LoginSteps extends BaseStep {
    
    private static Token tokenResponse;
    @SuppressWarnings("unused")
    private static String token;

    public LoginSteps(TestContext testContext){
        super(testContext);
    }
 
    
    @Given("^User is on login page$")
    public void user_is_on_login_Page() throws Throwable {
        System.out.println("User is in Login Page");
    }

    @When("^User enters \"(.*)\" and \"(.*)\"$")
    public void user_enters_UserName_and_Password(String username, String password) throws Throwable {
        AuthorizationRequest credentials = new AuthorizationRequest(username, password);
        tokenResponse = Endpoints.authenticateUser(credentials).getBody();
    }

    @Then("^User enters Home Page$")
    public void message_displayed_Login_Successfully() throws Throwable {
        token = tokenResponse.id;
        System.out.println("Done");
    }

}

BaseStep.java

package stepDefinition;

import apiEngine.EndPoints;
import cucumber.TestContext;

public class BaseStep {

    public EndPoints endPoints;

    public BaseStep(TestContext testContext) {
        testContext = new TestContext();
        System.out.println("I am in BaseStep!!!!!!!!!!\n");
        endPoints = testContext.getEndPoints();
    }
    
}

TestContext.java

package cucumber;

import apiEngine.EndPoints;

public class TestContext {
    
    private String BASE_URL = "http://localhost:3000/api/";
    public EndPoints endPoints;
    

    public TestContext() {
        System.out.println("I am in TextContext!!!!!!!!!!\n");
        if(endPoints == null) {
        endPoints = new EndPoints(BASE_URL);
        }
    }
    
     public EndPoints getEndPoints() {
        return endPoints;
    }
}

Endpoints.java

package apiEngine;

import apiEngine.model.requests.AddPhoneRequest;
import apiEngine.model.requests.AuthorizationRequest;
import apiEngine.model.responses.Phones;
import apiEngine.model.responses.Remove;
import apiEngine.model.responses.Token;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class Endpoints {
    private static String BASE_URL = "http://localhost:3000/api/" ;
    
    private static RequestSpecification request = null;
    Token tokenResponse;
    
    public Endpoints(String baseUrl) {
        System.out.println("I am in endpoints");
        BASE_URL = baseUrl;
        System.out.println(baseUrl);
        RestAssured.baseURI = baseUrl;
        request = RestAssured.given();
        request.header("Content-Type", "application/json");
    }
    
    public static IRestResponse<Token> authenticateUser(AuthorizationRequest credentials) {
            RestAssured.baseURI = BASE_URL;
            RequestSpecification request = RestAssured.given();
            request.header("Content-Type", "application/json");
            Response response = request.body(credentials).post(Route.generateToken());
            return new RestResponse<Token>(Token.class, response);
        }

    public static IRestResponse<Phones> addPhone(AddPhoneRequest addPhoneRequest,String token) {
        RestAssured.baseURI = BASE_URL;
        RequestSpecification request = RestAssured.given();
        request.header("Content-Type", "application/json").header("x-access-token", token);
        System.out.println("Hello");
        Response response = request.body(addPhoneRequest).post(Route.curd());
        return new RestResponse<Phones>(Phones.class,response);
    }
    
    public static Response getPhonesList(String token) {
        RestAssured.baseURI = BASE_URL;
        RequestSpecification request = RestAssured.given();
        request.header("Content-Type", "application/json").header("x-access-token", token);
        Response response = request.get(Route.curd());
        return response;
    }
    
    public static IRestResponse<Phones> getPhone(int Id,String token) {
        RestAssured.baseURI = BASE_URL;
        RequestSpecification request = RestAssured.given();
        request.header("Content-Type", "application/json").header("x-access-token", token);
        Response response = request.get(Route.curd(Id));
        return new RestResponse<Phones>(Phones.class,response);
    }
    
    public static IRestResponse<Phones> updatePhone(AddPhoneRequest updtaephonerequest, String token, int Id) {
        RestAssured.baseURI = BASE_URL;
        RequestSpecification request = RestAssured.given();
        request.header("Content-Type", "application/json").header("x-access-token", token);
        Response response = request.body(updtaephonerequest).put(Route.curd(Id));
        return new RestResponse<Phones>(Phones.class,response);
    }
    
    public static int getDeviceId(Phones[] phoneResponselist, String deviceName) {
        int Id = 0;
        for (int i = 0; i < phoneResponselist.length; i++) {
            String productname1 = phoneResponselist[i].name;
            if (productname1.equalsIgnoreCase(deviceName)) {
                Id = phoneResponselist[i].id;
            }
        }
        return Id;
    }
    
    public static IRestResponse<Remove> removePhone(int Id, String token) {
        RestAssured.baseURI = BASE_URL;
        RequestSpecification request = RestAssured.given();
        request.header("Content-Type", "application/json").header("x-access-token", token);
        Response response = request.delete(Route.curd(Id));
        return new RestResponse<Remove>(Remove.class,response);
    }
    
}

Note: Execution begins from LoginSteps.java

Arpit Aggarwal
  • 87
  • 6
  • 18

1 Answers1

0

This looks strange:

package stepDefinition;

import apiEngine.EndPoints;
import cucumber.TestContext;

public class BaseStep {

    public EndPoints endPoints;

    public BaseStep(TestContext testContext) {
        testContext = new TestContext();
        System.out.println("I am in BaseStep!!!!!!!!!!\n");
        endPoints = testContext.getEndPoints();
    }
    
}

First of all line testContext = new TestContext(); assigns an object to non-existing field so this code should not have been compiled..

UPD START: The line above has compiled because this reference is coming from your constructor argument :UPD END

Another point is that since you are using PicoContainer you should not create object by yourself. Container will do that for you. So your code should look like this:

package stepDefinition;

import apiEngine.EndPoints;
import cucumber.TestContext;

public class BaseStep {

    public EndPoints endPoints;
    TestContext testContext;

    public BaseStep(TestContext testContext) {
        this.testContext = testContext;
        System.out.println("I am in BaseStep!!!!!!!!!!\n");
        endPoints = testContext.getEndPoints();
    }
    
}
Alexey R.
  • 8,057
  • 2
  • 11
  • 27