javascript
带注释的控制器– Spring Web / Webflux和测试
Spring Webflux和Spring Web是兩個完全不同的Web堆棧。 但是, Spring Webflux繼續支持基于注釋的編程模型
使用這兩個堆棧定義的端點可能看起來相似,但是測試該端點的方式卻大不相同,并且編寫此端點的用戶必須知道哪個堆棧處于活動狀態并相應地制定測試。
樣本端點
考慮一個基于示例注釋的端點:
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestControllerdata class Greeting(val message: String)@RestController @RequestMapping("/web") class GreetingController {@PostMapping("/greet")fun handleGreeting(@RequestBody greeting: Greeting): Greeting {return Greeting("Thanks: ${greeting.message}")}}使用Spring Web進行測試
如果使用Spring Boot 2啟動程序以Spring Web作為啟動程序來創建此應用程序,請通過以下方式使用Gradle構建文件進行指定:
compile('org.springframework.boot:spring-boot-starter-web')那么將使用Mock Web運行時(稱為Mock MVC)對這種端點進行測試:
import org.junit.Test import org.junit.runner.RunWith import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import org.springframework.test.context.junit4.SpringRunner import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content@RunWith(SpringRunner::class) @WebMvcTest(GreetingController::class) class GreetingControllerMockMvcTest {@Autowiredlateinit var mockMvc: MockMvc@Testfun testHandleGreetings() {mockMvc.perform(post("/web/greet").content(""" |{|"message": "Hello Web"|}""".trimMargin())).andExpect(content().json("""|{|"message": "Thanks: Hello Web"|}""".trimMargin()))} }使用Spring Web-Flux進行測試
另一方面,如果引入了Spring-Webflux入門者,請使用以下Gradle依賴項進行說明:
compile('org.springframework.boot:spring-boot-starter-webflux')那么此端點的測試將使用出色的WebTestClient類,如下所示:
import org.junit.Test import org.junit.runner.RunWith import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest import org.springframework.http.HttpHeaders import org.springframework.test.context.junit4.SpringRunner import org.springframework.test.web.reactive.server.WebTestClient import org.springframework.web.reactive.function.BodyInserters@RunWith(SpringRunner::class) @WebFluxTest(GreetingController::class) class GreetingControllerTest {@Autowiredlateinit var webTestClient: WebTestClient@Testfun testHandleGreetings() {webTestClient.post().uri("/web/greet").header(HttpHeaders.CONTENT_TYPE, "application/json").body(BodyInserters.fromObject(""" |{| "message": "Hello Web"|}""".trimMargin())).exchange().expectStatus().isOk.expectBody().json("""|{| "message": "Thanks: Hello Web"|}""".trimMargin())} }結論
可以很容易地假設,由于使用Spring Web和Spring Webflux堆棧的編程模型看起來非常相似,因此使用Spring Web進行的這種遺留測試的測試將繼續到Spring Webflux,但是事實并非如此,作為我們的開發人員注意所使用的基礎堆棧并相應地制定測試。 我希望這篇文章闡明應該如何設計這樣的測試。
翻譯自: https://www.javacodegeeks.com/2017/12/annotated-controllers-spring-web-webflux-testing.html
總結
以上是生活随笔為你收集整理的带注释的控制器– Spring Web / Webflux和测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 公路赛摩托车电脑壁纸(摩托车赛车壁纸)
- 下一篇: 搜狗拼音中英切换快捷键(搜狗拼音输入法怎