网站报错403,无锡网站营销公司哪家好,福建老区建设网站,淄博做域名的公司spring 定时器注释Spring Webflux和Spring Web是两个完全不同的Web堆栈。 但是#xff0c; Spring Webflux继续支持基于注释的编程模型 使用这两个堆栈定义的端点可能看起来很相似#xff0c;但是测试这种端点的方式却完全不同#xff0c;并且编写这种端点的用户必须知道哪… 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.contentRunWith(SpringRunner::class)
WebMvcTest(GreetingController::class)
class GreetingControllerMockMvcTest {Autowiredlateinit var mockMvc: MockMvcTestfun 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.BodyInsertersRunWith(SpringRunner::class)
WebFluxTest(GreetingController::class)
class GreetingControllerTest {Autowiredlateinit var webTestClient: WebTestClientTestfun 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.htmlspring 定时器注释