日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试

發布時間:2023/12/3 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring 定時器注釋

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 定時器注釋

總結

以上是生活随笔為你收集整理的spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。