簡體   English   中英

使用 restAssured 測試 spring 啟動 rest 應用程序

[英]testing spring boot rest application with restAssured

我已經為此苦苦掙扎了一段時間。 我想使用 restAssured 來測試我的 SpringBoot REST 應用程序。

雖然看起來容器正常旋轉,但 rest 保證(其他任何東西似乎都無法接觸到它。

我一直收到 Connection refused 異常。

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)
...

我的測試 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);
    }

}

現在對於奇怪的部分, test通過並打印它應該打印的內容,但是test2正在獲取 Connection refused 異常。

任何想法這個設置有什么問題?

這個問題我自己來回答。。

在花費了額外的時間之后,結果證明TestRestTemplate已經知道並設置了正確的端口。 安心不...

有了這個,我到了下面的測試運行沒有任何問題的地步。

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

}

我可以發誓我以前嘗試過這樣做......但我想我確實使用了其他一些注釋......

基於https://stackoverflow.com/users/2838206/klubi答案,並且不要為您發出的每個請求設置端口:

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

您是否在某些非標准端口上運行? 你有沒有在你的

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

我建議在這種情況下使用@WebMvcTest ,您只需要放心模擬 mvc 依賴項:

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

使用@SpringBootTest來測試一個控制器是開銷,所有冗余 bean,如@Component@Service等,將被創建,一個完整的 HTTP 服務器將被啟動。 更多詳情: 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
     }
 }

看起來您正在嘗試為 Spring Web App 編寫集成測試。 REST-assured Support for Spring MockMvc on Baeldung提供了有關如何執行此操作的信息。

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

Baeldung 中沒有提到的是 Spring 的靜態導入更改。 關於 Bootstrapping Spring 的 REST-Assured 文檔
另一個 StackOverflow 中提到的導入問題

確保您使用:

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

不要與 Spring 一起使用:

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

使用錯誤的導入會導致連接被拒絕異常。

簡單地說:

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

我遇到了同樣的問題,服務器在端口 34965(不是 8080)上運行應用程序。

這解決了我的問題:

@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");
}

當我從@BeforeAll static JUnit 方法調用 RestAssured 時,我遇到了同樣的錯誤,所以我通過添加@TestInstance(TestInstance.Lifecycle.PER_CLASS)注釋使其成為非 static

@BeforeAll
public void setUp() {

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

代替

@BeforeAll
public static void setUp() {

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

這里得到的想法

"/clothes"作為參數傳遞給 get() 方法應該可以解決問題

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM