<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 使用`@WebFluxTest`和`WebTestClient`進行 Spring Boot Webflux 測試 > 原文: [https://howtodoinjava.com/spring-webflux/webfluxtest-with-webtestclient/](https://howtodoinjava.com/spring-webflux/webfluxtest-with-webtestclient/) 學習使用`@WebFluxTest`注解和`WebTestClient`來對 [spring boot webflux](https://howtodoinjava.com/spring-webflux/spring-webflux-tutorial/) 控制器進行單元測試,該測試用于通過 [**Junit 5**](https://howtodoinjava.com/spring-5-tutorial/) 測試 webflux 端點。 ## 1\. 使用`WebTestClient`的`@WebFluxTest` #### 1.1. Maven 依賴 添加[`reactor-test`](https://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22io.projectreactor%22%20AND%20a%3A%22reactor-test%22)依賴。 `pom.xml` ```java <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> ``` #### 1.2. `@WebFluxTest`注解 它需要完全自動配置,而僅應用與 WebFlux 測試相關的配置(例如`@Controller`,`@ControllerAdvice`,`@JsonComponent`,`Converter`和`WebFluxConfigurer` bean,但沒有`@Component`,`@Service`或`@Repository` bean)。 默認情況下,用`@WebFluxTest`注解的測試還將自動配置`WebTestClient`。 通常,`@WebFluxTest`與`@MockBean`或`@Import`結合使用以創建`@Controller` bean 所需的任何協作者。 > 要編寫需要完整應用程序上下文的集成測試,請考慮將`@SpringBootTest`與`@AutoConfigureWebTestClient`結合使用。 #### 1.3. `WebTestClient` 它是用于測試 Web 服務器的非阻塞式響應客戶端,該客戶端內部使用響應`WebClient`來執行請求,并提供流利的 API 來驗證響應。 它可以通過 HTTP 連接到任何服務器,或使用模擬請求和響應對象直接綁定到 WebFlux 應用程序,而無需 HTTP 服務器。 `WebTestClient`與`MockMvc`相似。 這些測試 Web 客戶端之間的唯一區別是`WebTestClient`旨在測試 WebFlux 端點。 ## 2\. 測試 webflux 控制器 #### 2.1. 用于 Webflux 控制器的 Junit 5 測試 在給定的示例中,我們正在測試`EmployeeController`類,該類包含用于 CRUD 操作的指令方法。 `EmployeeControllerTest.java` ```java import static org.mockito.Mockito.times; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.reactive.function.BodyInserters; import com.howtodoinjava.demo.controller.EmployeeController; import com.howtodoinjava.demo.dao.EmployeeRepository; import com.howtodoinjava.demo.model.Employee; import com.howtodoinjava.demo.service.EmployeeService; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @ExtendWith(SpringExtension.class) @WebFluxTest(controllers = EmployeeController.class) @Import(EmployeeService.class) public class EmployeeControllerTest { @MockBean EmployeeRepository repository; @Autowired private WebTestClient webClient; @Test void testCreateEmployee() { Employee employee = new Employee(); employee.setId(1); employee.setName("Test"); employee.setSalary(1000); Mockito.when(repository.save(employee)).thenReturn(Mono.just(employee)); webClient.post() .uri("/create") .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromObject(employee)) .exchange() .expectStatus().isCreated(); Mockito.verify(repository, times(1)).save(employee); } @Test void testGetEmployeesByName() { Employee employee = new Employee(); employee.setId(1); employee.setName("Test"); employee.setSalary(1000); List<Employee> list = new ArrayList<Employee>(); list.add(employee); Flux<Employee> employeeFlux = Flux.fromIterable(list); Mockito .when(repository.findByName("Test")) .thenReturn(employeeFlux); webClient.get().uri("/name/{name}", "Test") .header(HttpHeaders.ACCEPT, "application/json") .exchange() .expectStatus().isOk() .expectBodyList(Employee.class); Mockito.verify(repository, times(1)).findByName("Test"); } @Test void testGetEmployeeById() { Employee employee = new Employee(); employee.setId(100); employee.setName("Test"); employee.setSalary(1000); Mockito .when(repository.findById(100)) .thenReturn(Mono.just(employee)); webClient.get().uri("/{id}", 100) .exchange() .expectStatus().isOk() .expectBody() .jsonPath("$.name").isNotEmpty() .jsonPath("$.id").isEqualTo(100) .jsonPath("$.name").isEqualTo("Test") .jsonPath("$.salary").isEqualTo(1000); Mockito.verify(repository, times(1)).findById(100); } @Test void testDeleteEmployee() { Mono<Void> voidReturn = Mono.empty(); Mockito .when(repository.deleteById(1)) .thenReturn(voidReturn); webClient.get().uri("/delete/{id}", 1) .exchange() .expectStatus().isOk(); } } ``` * 我們正在使用`@ExtendWith( SpringExtension.class )`支持 Junit 5 中的測試。在 Junit 4 中,我們需要使用`@RunWith(SpringRunner.class)`。 * 我們使用`@Import(EmployeeService.class)`為應用程序上下文提供服務依賴,而使用`@WebFluxTest`時不會自動掃描該上下文。 * 我們模擬了`EmployeeRepository`類型為`ReactiveMongoRepository`的模型。 這將阻止實際的數據庫插入和更新。 * `WebTestClient`用于命中控制器的特定端點并驗證其是否返回正確的狀態代碼和主體。 #### 2.2. 測試 Spring Boot Webflux 控制器 作為參考,讓我們看看上面已經測試過的控制器。 `EmployeeController.java` ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import com.howtodoinjava.demo.model.Employee; import com.howtodoinjava.demo.service.EmployeeService; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @RestController public class EmployeeController { @Autowired private EmployeeService employeeService; @PostMapping(value = { "/create", "/" }) @ResponseStatus(HttpStatus.CREATED) public void create(@RequestBody Employee e) { employeeService.create(e); } @GetMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) public ResponseEntity<Mono<Employee>> findById(@PathVariable("id") Integer id) { Mono<Employee> e = employeeService.findById(id); HttpStatus status = (e != null) ? HttpStatus.OK : HttpStatus.NOT_FOUND; return new ResponseEntity<>(e, status); } @GetMapping(value = "/name/{name}") @ResponseStatus(HttpStatus.OK) public Flux<Employee> findByName(@PathVariable("name") String name) { return employeeService.findByName(name); } @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE) @ResponseStatus(HttpStatus.OK) public Flux<Employee> findAll() { return employeeService.findAll(); } @PutMapping(value = "/update") @ResponseStatus(HttpStatus.OK) public Mono<Employee> update(@RequestBody Employee e) { return employeeService.update(e); } @DeleteMapping(value = "/delete/{id}") @ResponseStatus(HttpStatus.OK) public void delete(@PathVariable("id") Integer id) { employeeService.delete(id).subscribe(); } } ``` 請使用`@WebFluxTest`和`WebTestClient`將有關 **spring webflux 控制器單元測試**的問題交給我。 學習愉快! [下載源碼](https://github.com/lokeshgupta1981/SpringExamples/blob/master/spring-webflux-demo/src/test/java/com/howtodoinjava/demo/EmployeeControllerTest.java)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看