Rest-assured: when using static requestSpecification, static baseUri is ignored (if it has been set after)

Created on 21 Jul 2015  路  5Comments  路  Source: rest-assured/rest-assured

_From [email protected] on November 18, 2014 21:30:16_

What steps will reproduce the problem? Run a test that looks like:
@Test
public void staticBaseUriNotTakenIntoAccount() {
RestAssured.requestSpecification = new RequestSpecBuilder().build();
RestAssured.baseURI = " https://code.google.com/"; RestAssured.given()
.log().all()
.when()
.get("/p/rest-assured")
.then()
.log().all()
.statusCode(200);
} What is the expected output? What do you see instead? I expect the request to be a GET on https://code.google.com/p/rest-assured .

Instead, I see a logged request on http://localhost:8080/p/rest-assured It looks like this:
Request method: GET
Request path: http://localhost:8080/p/rest-assured
Proxy:
Request params:
Query params:
Form params:
Path params:
Multiparts:
Headers: Accept=_/_
Cookies:
Body:

The rest of the log is:
15:17:15.053 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Get connection for route {}->http://localhost:8080
15:17:15.067 [main] DEBUG o.a.h.i.c.DefaultClientConnectionOperator - Connecting to localhost:8080
15:17:16.067 [main] DEBUG o.a.h.i.c.DefaultClientConnectionOperator - Connect to localhost:8080 timed out. Connection will be retried using another IP address
15:17:16.067 [main] DEBUG o.a.h.i.c.DefaultClientConnectionOperator - Connecting to localhost:8080
15:17:17.068 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@cdc3aae closed
15:17:17.068 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@cdc3aae shut down
15:17:17.068 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@7ef2d7a6 What version of the product are you using? On what operating system? I am using v2.4.0, on Windows 8.1

Note: alternatively, if I set RestAssured.baseURI BEFORE setting RestAssured.requestSpecification, the request hits the right endpoint (GET https://code.google.com/p/rest-assured/ )

Note: another workaround, and possibly a cleaner way to do it, is to NOT use the static RestAssured.baseUri field at all but use the baseUri() method on the RequestSpecification class instead.

_Original issue: http://code.google.com/p/rest-assured/issues/detail?id=373_

Priority-Medium imported bug

Most helpful comment

I encountered this issue today and, at least in my case, worked out the cause.

After setting up the RequestSpec and ResponseSpec in a @BeforeClass setup() method, I encountered the issue. I was assigning the specs to RestAssured implicitly, but while investigating I I tried adding a given().spec(spec) inline in the test method body explicitly. That was when I got, java.lang.IllegalArgumentException: Specification to merge with cannot be null. I realized then that setup() was not getting called.

Looking further, I noticed that instead of importing org.testng.annotations.BeforeClass I had imported org.junit.BeforeClass by mistake. I was working in TestNG, so the JUnit @BeforeClass was not triggering the setup(), so the request specification was being evaluated as null in the testcase method body. Instead of leading to some kind of NPE as I would have expected, the Rest Assured framework was quietly falling back to default host/port settings instead. As soon as I changed the import type of BeforeClass, then everything started working.

All 5 comments

_From [email protected] on November 27, 2014 05:00:00_

thanks for reporting

Status: Accepted

Hi @johanhaleby - please could you update the plan for addressing this issue ?

Thanks.

@sachinlala Sorry I don't have a plan. If it's important for you then please help out with a PR :)

I encountered this issue today and, at least in my case, worked out the cause.

After setting up the RequestSpec and ResponseSpec in a @BeforeClass setup() method, I encountered the issue. I was assigning the specs to RestAssured implicitly, but while investigating I I tried adding a given().spec(spec) inline in the test method body explicitly. That was when I got, java.lang.IllegalArgumentException: Specification to merge with cannot be null. I realized then that setup() was not getting called.

Looking further, I noticed that instead of importing org.testng.annotations.BeforeClass I had imported org.junit.BeforeClass by mistake. I was working in TestNG, so the JUnit @BeforeClass was not triggering the setup(), so the request specification was being evaluated as null in the testcase method body. Instead of leading to some kind of NPE as I would have expected, the Rest Assured framework was quietly falling back to default host/port settings instead. As soon as I changed the import type of BeforeClass, then everything started working.

If someone using jUnit then replace @BeforeClass with @BeforeEach, it will work, good luck have fun

Was this page helpful?
0 / 5 - 0 ratings