29

I've been struggling with this for some time now. I'd like to use restAssured to test my SpringBoot REST application.

While it looks like container spins up properly, rest assured (and anything else seems to have problems reaching out to it.

All the time I'm getting Connection refused exception.

java.net.ConnectException: Connection refused

at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
...

my test class:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SizesRestControllerIT {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void test() {
        System.out.println(this.restTemplate.getForEntity("/clothes", List.class));
    }

    @Test
    public void test2() throws InterruptedException {
        given().basePath("/clothes").when().get("").then().statusCode(200);
    }

}

and now for the weird part, test passes and prints what it should, but test2 is getting Connection refused exception.

Any ideas what is wrong with this setup?

klubi
  • 1,373
  • 1
  • 14
  • 25

9 Answers9

51

I'll answer this question myself..

After spending additional amount of time on it it turned out that TestRestTemplate already knows and sets proper port. RestAssured does not...

With that I got to a point where below test runs without any issues.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SizesRestControllerIT {

    @LocalServerPort
    int port;

    @Before
    public void setUp() {
        RestAssured.port = port;
    }

    @Test
    public void test2() throws InterruptedException {
        given().basePath("/clothes").get("").then().statusCode(200);
    }

}

I could have sworn I tried doing it this way previously... But I guess I did use some other annotations with this...

Rémi Benoit
  • 1,307
  • 11
  • 17
klubi
  • 1,373
  • 1
  • 14
  • 25
  • 1
    Note that with Spring 2 this works with minor modification: `@ExtendWith(SpringExtension.class)` instead of `RunWith` and `@BeforeEach` instead of `Before` when using Junit5 (Jupiter). – Randy May 12 '20 at 08:47
  • This and other answers don't take into account that using RANDOM_PORT will kill Spring Test's transaction management. @Transactional won't work and tests won't be isolated anymore. This will present problems in the long run. https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications – Martín Coll Oct 15 '20 at 14:08
7

Based on https://stackoverflow.com/users/2838206/klubi answer and to not set the port for every request that you make:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SizesRestControllerIT {

    @LocalServerPort
    int port;

    @Before
    public void setUp() {
        RestAssured.port = port;
    }

    @Test
    public void test2() throws InterruptedException {
        given().basePath("/clothes").get("").then().statusCode(200);
    }
}
Mateus Costa
  • 103
  • 3
  • 5
  • 1
    Note that with Spring 2 this works with minor modification: `@ExtendWith(SpringExtension.class)` instead of `RunWith` and `@BeforeEach` instead of `Before` when using Junit5 (Jupiter). – Randy May 12 '20 at 08:47
4

are you running on some non-standard port may be? have you tried this in your

@Before public static void init(){ RestAssured.baseURI = "http://localhost"; // replace as appropriate RestAssured.port = 8080; }

ameet chaubal
  • 1,440
  • 16
  • 37
  • `SpringBootTest.WebEnvironment.RANDOM_PORT` causes app to run on different port each time – klubi Nov 17 '16 at 22:26
  • then may be, ` @Value("${local.server.port}") int port; @Before public static void init(){ RestAssured.baseURI = "http://localhost"; // replace as appropriate RestAssured.port = port; } ` – ameet chaubal Nov 18 '16 at 20:43
2

I'd recommend to use @WebMvcTest for that case, all you need is to have rest assured mock mvc dependency:

<dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>spring-mock-mvc</artifactId>
            <version>${rest-assured.version}</version>
            <scope>test</scope>
</dependency>

Using of @SpringBootTest to test just a controller is overhead, all redundant beans, like @Component, @Service, etc, will be created and a full HTTP server will be started. For more details: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests;

  @RunWith(SpringRunner.class)
  @WebMvcTest(value = SizesRestController.class)
  public class SizesRestControllerIT {

     @Autowired
     private MockMvc mvc;

     @Before
     public void setUp() {
        RestAssuredMockMvc.mockMvc(mvc);
     }

     @Test
     public void test() {
        RestAssuredMockMvc.given()
           .when()
           .get("/clothes")
           .then()
           .statusCode(200);
        // do some asserts
     }
 }
idmitriev
  • 4,619
  • 4
  • 28
  • 44
  • Now you using mock/unit (out-of-container) testing, however, the question is asking about how to do integration (end-to-end) testing – Muhammad Hewedy Apr 24 '19 at 05:39
1

It looks like you are trying to write an integration test for a Spring Web App. REST-assured Support for Spring MockMvc on Baeldung has information on how to do this.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class SizesRestControllerIT {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void initialiseRestAssuredMockMvcWebApplicationContext() {
        RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
    }

    @Test
    public void test2() throws InterruptedException {
        given().basePath("/clothes").get("").then().statusCode(200);
    }
}

What is not mentioned in Baeldung is the static imports change for Spring. REST-Assured Docs on Bootstrapping Spring
Import issues mentioned in another StackOverflow

Make sure you use:

import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;
import static io.restassured.module.mockmvc.matcher.RestAssuredMockMvcMatchers.*;

Do not use with Spring:

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;

Using the wrong imports can cause the connection refused exception.

William Jones
  • 61
  • 1
  • 7
0

Simply:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT)
public class CommonScenarioTest {

    @BeforeClass
    public static void setup() {
        RestAssured.baseURI = "http://localhost/foo";
        RestAssured.port = 8090;
    }
erik
  • 1
0

I had the same issue, The server was running the App on the the port 34965 (not 8080).

This solved my problem :

@Autowired
ServerProperties serverProperties;

@Autowired
Environment environment;

public String getPath() {
    final int port = environment.getProperty("local.server.port", Integer.class);

    return "http://localhost:" + port;
}

@Before
public void setUp() throws Exception {
    RestAssured.baseURI = getPath();
}

@Test
public void testResponse(){
    response = get("/books");
}
Amine Soumiaa
  • 683
  • 5
  • 13
0

I got this same error when I called RestAssured from the @BeforeAll static JUnit method, so I made it non static by adding the @TestInstance(TestInstance.Lifecycle.PER_CLASS) annotation

@BeforeAll
public void setUp() {

    accessToken = getAuthorizationToken();
    Util.deleteAllFromPassThroughCourseWorkList(accessToken);
}

instead of

@BeforeAll
public static void setUp() {

    accessToken = getAuthorizationToken();
    Util.deleteAllFromPassThroughCourseWorkList(accessToken);
}

Got the idea from here

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
-2

Passing "/clothes" as parameter to get() method should resolve the issue

@Test
public void test2() throws InterruptedException {
    when().
        get("/clothes").
    then().
        statusCode(200);
}
Issam El-atif
  • 2,366
  • 2
  • 17
  • 22