Skip to content

Commit

Permalink
Add test for global Consumer<ExchangeResult> in WebTestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Mar 15, 2021
1 parent 6214ff1 commit 6e264f9
Showing 1 changed file with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.reactive.server.samples;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests with a globally registered
* {@link org.springframework.test.web.reactive.server.EntityExchangeResult} consumer.
*
* @author Rossen Stoyanchev
*/
public class GlobalEntityResultConsumerTests {

private final StringBuilder output = new StringBuilder();

private final WebTestClient client = WebTestClient.bindToController(TestController.class)
.configureClient()
.entityExchangeResultConsumer(result -> {
byte[] bytes = result.getResponseBodyContent();
this.output.append(new String(bytes, StandardCharsets.UTF_8));
})
.build();


@Test
void json() {
this.client.get().uri("/person/1")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectBody().json("{\"name\":\"Joe\"}");

assertThat(this.output.toString()).isEqualTo("{\"name\":\"Joe\"}");
}

@Test
void entity() {
this.client.get().uri("/person/1")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectBody(Person.class).isEqualTo(new Person("Joe"));

assertThat(this.output.toString()).isEqualTo("{\"name\":\"Joe\"}");
}

@Test
void entityList() {
this.client.get().uri("/persons")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectBodyList(Person.class).hasSize(2);

assertThat(this.output.toString())
.isEqualTo("[{\"name\":\"Joe\"},{\"name\":\"Joseph\"}]");
}


@RestController
static class TestController {

@GetMapping("/person/{id}")
Person getPerson() {
return new Person("Joe");
}

@GetMapping("/persons")
List<Person> getPersons() {
return Arrays.asList(new Person("Joe"), new Person("Joseph"));
}
}
}

0 comments on commit 6e264f9

Please sign in to comment.