testing spring boot rest application with restAssu

2019-02-11 21:06发布

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?

7条回答
甜甜的少女心
2楼-- · 2019-02-11 21:29

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;
    }
查看更多
叛逆
3楼-- · 2019-02-11 21:31

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

@Test
public void test2() throws InterruptedException {
    when().
        get("/clothes").
    then().
        statusCode(200);
}
查看更多
Ridiculous、
4楼-- · 2019-02-11 21:36

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
     }
 }
查看更多
放荡不羁爱自由
5楼-- · 2019-02-11 21:40

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);
    }
}
查看更多
Luminary・发光体
6楼-- · 2019-02-11 21:41

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");
}
查看更多
干净又极端
7楼-- · 2019-02-11 21:44

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; }

查看更多
登录 后发表回答