0

在 Cucumber 中共享测试上下文,同时在 java 中创建对象以在所有场景中共享相同的状态我得到异常

在这里,我正在从 TestContext 类创建端点类的对象

故障追踪

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)

登录步骤.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

包装黄瓜;

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

端点.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);
    }
    
}

注意:从 LoginSteps.java 开始执行

4

1 回答 1

0

这看起来很奇怪:

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 = new TestContext();对象分配给不存在的字段,因此不应编译此代码..

UPD START:上面的行已编译,因为此引用来自您的构造函数参数:UPD END

另一点是,既然你正在使用PicoContainer,你不应该自己创建对象。Container 会为您做到这一点。所以你的代码应该是这样的:

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();
    }
    
}
于 2020-08-31T11:28:06.937 回答