Problem after moving RequestSpecification code in @BeforeClass section
Below code works fine for me:
@Test (priority=1, dependsOnMethods = {"checkIfAllServicesAreUp"})
public void verifyCreateUser() {
RestAssured.baseURI = "someValidURI";
RestAssured.basePath = "userservice/user/";
RequestSpecification spec = new RequestSpecBuilder().setContentType(ContentType.JSON).log(LogDetail.METHOD).build();
response = RestAssured.given().spec(spec).headers("source","APP").body("{ }").when().post("");
}
But when I move the linked code RequestSpecification
below @BeforeClass
like this:
private RequestSpecification spec;
@BeforeClass
public void setSpec() {
spec = new RequestSpecBuilder().setContentType(ContentType.JSON).log(LogDetail.METHOD).build();
}
@Test (priority=1, dependsOnMethods = {"checkIfAllServicesAreUp"})
public void verifyCreateUser() {
RestAssured.baseURI = "someValidURI";
RestAssured.basePath = "userservice/user/";
response = RestAssured.given().spec(spec).headers("source","APP").body("{ }").when().post("");
}
My API test is returning error code 405 (method not allowed).
It seems to spec
override the assignment RestAssured.basePath
inside my test method verifyCreateUser
, since I don't set it explicitly in spec
explicitly, and the POST call gets caught in someValidURI
instead someValidURI+/userservice/user
and hence the error code is 405. I don't want to set basePath
in spec
, since it will be different for each of my methods testing. Please help find an elegant solution here.
source to share
Changed my code as below and now it works fine:
@Test (priority=1, dependsOnMethods = {"checkIfAllServicesAreUp"})
public void verifyCreateUser() {
RestAssured.baseURI = "someValidURI";
RequestSpecification spec = new RequestSpecBuilder().setBasePath("userservice/user/").setContentType(ContentType.JSON).log(LogDetail.METHOD).build();
response = RestAssured.given().spec(spec).headers("source","APP").body("{ }").when().post("");
}
Apparently the way I set up basePath
per test was flawed earlier. Now I'll do it in spec
.
Hope this helps someone in the future.
source to share