javascript
spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试
spring 定時器注釋
Spring Webflux和Spring Web是兩個完全不同的Web堆棧。 但是, Spring Webflux繼續(xù)支持基于注釋的編程模型
使用這兩個堆棧定義的端點可能看起來很相似,但是測試這種端點的方式卻完全不同,并且編寫這種端點的用戶必須知道哪個堆棧處于活動狀態(tài)并相應(yīng)地制定測試。
樣本端點
考慮一個基于示例注釋的端點:
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作為啟動程序來創(chuàng)建此應(yīng)用程序,請通過以下方式使用Gradle構(gòu)建文件指定該啟動程序:
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())} }結(jié)論
可以很容易地假設(shè),由于使用Spring Web和Spring Webflux堆棧的編程模型看起來非常相似,因此使用Spring Web進行的這種遺留測試的測試將繼續(xù)到Spring Webflux,但是事實并非如此,作為開發(fā)人員注意所使用的基礎(chǔ)堆棧并相應(yīng)地制定測試。 我希望這篇文章闡明如何設(shè)計這樣的測試。
翻譯自: https://www.javacodegeeks.com/2017/12/annotated-controllers-spring-web-webflux-testing.html
spring 定時器注釋
總結(jié)
以上是生活随笔為你收集整理的spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星w21和w23(三星w21和w202
- 下一篇: 夕阳唯美电脑主题(夕阳电脑壁纸高清)