본문 바로가기

Spring/Spring Boot(스프링 부트)

WebFlux test get, get with param, post, post with param

728x90

Parameter 2개 이상 보내기

@RestController
@RequestMapping(value = "/api/v1/order")
public class DistributeController {

    // http://localhost:8080/api/v1/order/distribute?id=1111&name=krk 이렇게 요청한다.
    @GetMapping(value = "/distribute")
    public String getOrder(@RequestParam int id, @RequestParam String name) {
        System.out.println(id);
        System.out.println(name);
}

결과

1111
krk

TransferController.java

@Slf4j
@RestController
@RequestMapping("/api/v1")
public class TransferController {

    private TransferService transferService;

    public TransferController(TransferService transferService) {
        this.transferService = transferService;
    }

    @GetMapping("/transfer")
    Mono<ServerResponse> getTransfer(ServerRequest req) {
        return ok().body(fromObject(req.pathVariable("name")));
    }

    @GetMapping("/transferp")
    Mono<String> getTransferp() {
        return Mono.just("hello");
    }

    @GetMapping("/transfer2/{name}")
    Mono<String> getTransfer2(@PathVariable String name) {
        return Mono.just("hello" + name);
    }

    @GetMapping("/transfer3")
    Mono<String> getTransfer3(Transfer transfer) {
        return Mono.just("hello" + transfer.getDirection());
    }

    // Mono로 받으면 좋은점 .map등을 쓸 수 있음
    @GetMapping("/transfer4")
    Mono<String> getTransfer4(@RequestBody Mono<Transfer> transfer) {
        return transfer.map(t -> "hello" + t.getDirection());
    }

    @PostMapping(value = "/transfer5")
    Mono<String> getTransfer5(@RequestBody Flux<Transfer> transfer) {
        return Mono.just("hello4");
    }

    @PostMapping(value = "/transfer6")
    Mono<String> getTransfer6(@RequestBody Transfer transfer) {
        transferService.saveToDb(transfer);
        log.info("=>{}", transfer);
        return Mono.just("hello-withdraw");
    }
}

TransferControllerTest

@RunWith(SpringRunner.class)
@WebFluxTest
public class TransferControllerTest {

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void getWithParam() {
        webTestClient.get().uri("/api/v2/transfer2/{name}", "2")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello" + "2");
    }

    @Test
    public void get() {
        webTestClient.post().uri("/api/v2/transfer3")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello");
    }


    @Test
    public void post() {
        webTestClient.post().uri("/api/v2/transfer5")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello" + "2");
    }

    @Test
    public void postWithParam() {
        webTestClient.post().uri("/api/v2/transfer6")
                .body(BodyInserters.fromObject(new Transfer(LocalDateTime.now(), BigDecimal.TEN, "aa", "withdraw")))
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello-" + "withdraw");
    }
}
728x90
블로그 주인장입니다. 원하시는 정보는 얻으셨나요? 이 포스트에서 추가로 필요한 정보가 있으시면 여기에 남겨주세요.