0

I created a json in my local machine and tried to access it via the server like below

It is working in the Postman but when I try to run through the code it is giving java.net.ConnectException: Connection refused: connect error. Code as follows

public class GetUsingLocalJson {
    @Test
    void testGet() throws Exception {
        given().when().get("http://localhost:3000/students").then().log().all();
    }
}

Can someone help me why it is failing in the code? imports I have used is below

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
hitman45
  • 3
  • 2
  • Does this answer your question? [RestAssuredMockMvc Connection to http://localhost:8080 refused](https://stackoverflow.com/questions/23782160/restassuredmockmvc-connection-to-http-localhost8080-refused) – Alexey R. May 16 '23 at 07:29

1 Answers1

0

Set port as RestAssured.port = 3000; localhost is set as baseURI by default. Then simply call your endpoint like so:

given().when().get("/students").then().log().all();
Maverick
  • 61
  • 1
  • 5
  • Where to add RestAssured.port = 3000;? – hitman45 Jun 15 '23 at 10:54
  • for example in a startup method if you have class dedicated to RestAssured config. But it can simply be at the top of the test method, before your `given()...` expression. [rest assured default values](https://github.com/rest-assured/rest-assured/wiki/Usage#default-values) ```public class GetUsingLocalJson { @Test void testGet() throws Exception { RestAssured.port = 3000; given().when().get("http://localhost:3000/students").then().log().all(); } }``` – Maverick Jun 16 '23 at 08:56