Java Code Examples for org.springframework.mock.web.reactive.function.server.MockServerRequest

Following code examples demonstrate how to use org.springframework.mock.web.reactive.function.server.MockServerRequestfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.mock.web.reactive.function.server.MockServerRequestand various code implementation of this class.

    public void saveUser() {
        User user = TestModelFactory.createUser();
        MockServerRequest postRequest = MockServerRequest.builder().method(HttpMethod.POST).body(Mono.just(user));
        MockServerRequest putRequest = MockServerRequest.builder().method(HttpMethod.PUT).body(Mono.just(user));
        given(userService.save(user)).willReturn(Mono.empty());

        Mono<ServerResponse> postResponse = handler.save(postRequest);
        Mono<ServerResponse> putResponse = handler.save(putRequest);

        StepVerifier.create(postResponse)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.CREATED);
                    assertThat(x.headers().get("Location").get(0)).isEqualTo("/api/user");
                })
                .expectComplete()
                .verify();

        StepVerifier.create(putResponse)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();

    } 


    public void getUser() {
        User user = TestModelFactory.createUser();
        user.setId("testid");
        MockServerRequest request = MockServerRequest.builder().pathVariable("id", user.getId()).build();
        given(userService.get(user.getId())).willReturn(Mono.just(user));
        Mono<ServerResponse> response = handler.get(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void deleteUser() {
        MockServerRequest request = MockServerRequest.builder().body(Mono.just("testId"));
        given(userService.delete(any())).willReturn(Mono.empty());
        Mono<ServerResponse> response = handler.delete(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void getClub() {
        MockServerRequest request = MockServerRequest.builder().pathVariable("id", "testId").build();
        given(userService.getClub("testId")).willReturn(Mono.just(new Club("name", 10, new Date())));
        Mono<ServerResponse> response = handler.getClub(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void getUserList() {
        MockServerRequest request = MockServerRequest.builder().pathVariable("page", "0").build();
        given(userService.getList(0)).willReturn(Flux.empty());
        Mono<ServerResponse> response = handler.getList(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void getUserList() {
        MockServerRequest request = MockServerRequest.builder().pathVariable("page", "0").build();
        given(clubService.getList(0)).willReturn(Flux.empty());
        Mono<ServerResponse> response = handler.getList(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void saveClub() {
        Club club = TestModelFactory.createClub();
        MockServerRequest postRequest = MockServerRequest.builder().method(HttpMethod.POST).body(Mono.just(club));
        given(clubService.save(club)).willReturn(Mono.empty());

        Mono<ServerResponse> postResponse = handler.save(postRequest);

        StepVerifier.create(postResponse)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.CREATED);
                    assertThat(x.headers().get("Location").get(0)).isEqualTo("/api/club");
                })
                .expectComplete()
                .verify();

    } 

    public void join() {
        Map<String, String> map = new HashMap<>();
        MockServerRequest request = MockServerRequest.builder().body(Mono.just(map));
        given(clubService.join(any(), any())).willReturn(Mono.just(JoinResult.SUCCESS));
        Mono<ServerResponse> response = handler.join(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void delete() {
        MockServerRequest request = MockServerRequest.builder().body(Mono.just("testId"));
        given(clubService.delete(any())).willReturn(Mono.empty());
        Mono<ServerResponse> response = handler.delete(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void join_not_allow_age() {
        Map<String, String> map = new HashMap<>();
        MockServerRequest request = MockServerRequest.builder().body(Mono.just(map));
        given(clubService.join(any(), any())).willReturn(Mono.just(JoinResult.FAIL_NOT_ALLOW_AGE));
        Mono<ServerResponse> response = handler.join(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
                })
                .expectComplete()
                .verify();
    } 

    public void leave() {
        UserId m = new UserId("user");
        MockServerRequest request = MockServerRequest.builder().body(Mono.just(m));
        given(clubService.leave(any())).willReturn(Mono.just(new User()));
        Mono<ServerResponse> response = handler.leave(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void leave_user_not_found_then_404() {
        MockServerRequest request = MockServerRequest.builder().body(Mono.empty());
        given(clubService.leave(any())).willReturn(Mono.just(new User()));
        Mono<ServerResponse> response = handler.leave(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.NOT_FOUND);
                })
                .expectComplete()
                .verify();
    } 

    public void get() {
        MockServerRequest request = MockServerRequest.builder().pathVariable("id", "testId").build();
        Mono<Club> mockResult = Mono.just(new Club("name", 10, new Date()));
        given(clubService.get("testId")).willReturn(mockResult);
        Mono<ServerResponse> response = handler.get(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.OK);
                })
                .expectComplete()
                .verify();
    } 

    public void join_not_found() {
        Map<String, String> map = new HashMap<>();
        MockServerRequest request = MockServerRequest.builder().body(Mono.just(map));
        given(clubService.join(any(), any())).willReturn(Mono.just(JoinResult.NOT_FOUND));
        Mono<ServerResponse> response = handler.join(request);
        StepVerifier.create(response)
                .consumeNextWith(x -> {
                    assertThat(x.statusCode()).isEqualTo(HttpStatus.NOT_FOUND);
                })
                .expectComplete()
                .verify();
    } 

		public void normalConfigurationFails() {

				MockServerRequest request = MockServerRequest
					.builder()
					.uri(URI.create("/CATS"))
					.build();

				RequestPredicate predicate = GET("/cats");

				Assertions.assertThat(predicate.test(request)).isFalse();
		} 

		public void ciLowercasePredicateUppercaseRequest() {
				RequestPredicate predicate = new CaseInsensitiveRequestPredicate(GET("/cats"));
				MockServerRequest request = MockServerRequest.builder().uri(URI.create("/CATS")).build();
				Assertions.assertThat(predicate.test(request)).isTrue();
		} 

    public void idFromPath_shouldRetrieveId() {
        ObjectId id = new ObjectId();
        MockServerRequest request = MockServerRequest.builder().pathVariable("id", id.toString()).build();
        assertThat(RequestUtils.idFromPath(request)).isEqualTo(id);
    } 

Advertisement
Javadoc
Mock implementation of ServerRequest. @author Arjen Poutsma @since 5.0
Advertisement