问题描述
在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)
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
包装黄瓜;
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",token);
Response response = request.get(Route.curd());
return response;
}
public static IRestResponse<Phones> getPhone(int Id,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",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,token);
Response response = request.delete(Route.curd(Id));
return new RestResponse<Remove>(Remove.class,response);
}
}
注意: 执行从LoginSteps.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 = new TestContext();
行将一个对象分配给不存在的字段,因此不应编译该代码。
UPD START :上面的行已编译,因为此引用来自您的构造函数参数: UPD END
另一点是,由于您正在使用PicoContainer
,因此您不应该自己创建对象。容器将为您做到这一点。因此您的代码应如下所示:
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();
}
}