Last active
May 10, 2022 10:31
-
-
Save mskonovalov/42761bbc548e92c2af16c40cffcfcaf3 to your computer and use it in GitHub Desktop.
Spring 5.0 web-flux test Java/Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.Data; | |
import org.reactivestreams.Publisher; | |
import org.springframework.web.bind.annotation.*; | |
import reactor.core.publisher.Flux; | |
import reactor.core.publisher.Mono; | |
@RestController | |
public class Controller { | |
@PostMapping("/person") | |
Mono<Void> create(@RequestBody Publisher<Person> personStream) { | |
return Mono.empty(); | |
} | |
@GetMapping("/person") | |
Flux<Person> list() { | |
return Flux.just(new Person("123", "Ivan")); | |
} | |
@GetMapping("/person/{id}") | |
Mono<Person> findById(@PathVariable String id) { | |
return Mono.just(new Person(id, "Ivan")); | |
} | |
@Data | |
public static class Person { | |
private final String id; | |
private final String name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Test; | |
import org.springframework.http.MediaType; | |
import org.springframework.test.web.reactive.server.WebTestClient; | |
import org.springframework.test.web.reactive.server.WebTestClient.BodySpec; | |
public class ControllerTest { | |
private WebTestClient client = WebTestClient | |
.bindToController(new Controller()) | |
.build(); | |
@Test | |
public void testGet1() { | |
BodySpec<Person, ?> personBodySpec = client.get().uri("/person/42") | |
.exchange() | |
.expectStatus().isOk() | |
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) | |
.expectBody(Person.class); | |
personBodySpec.isEqualTo(new Person("42", "Ivan")); | |
} | |
@Test | |
public void testGet2() { | |
BodySpec<Person, ?> personBodySpec = client.get().uri("/person/27") | |
.exchange() | |
.expectStatus().isOk() | |
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) | |
.expectBody(Person.class); | |
personBodySpec.consumeWith(person -> Assert.assertEquals("27", person.getResponseBody().getId())); | |
} | |
@Test | |
public void testList() { | |
client.get().uri("/person") | |
.exchange() | |
.expectStatus().isOk() | |
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) | |
.expectBodyList(Person.class).consumeWith(list -> Assert.assertEquals(list.getResponseBody().size(), 1)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert | |
import org.junit.Test | |
import org.springframework.http.MediaType | |
import org.springframework.test.web.reactive.server.WebTestClient | |
import org.springframework.test.web.reactive.server.WebTestClient.BodySpec | |
import org.springframework.test.web.reactive.server.WebTestClient.ListBodySpec | |
class ControllerTest2 { | |
private val client = WebTestClient | |
.bindToController(Controller()) | |
.build() | |
@Test | |
fun `test get`() { | |
val expectBody: BodySpec<Person, *> = client.get().uri("/person/42").exchange() | |
.expectBody(Person::class.java) | |
expectBody.isEqualTo(Person("42", "Ivan")) // doesn't compile here | |
expectBody.isEqualTo<BodySpec<Person, *>>(Person("42", "Ivan")) // doesn't compile here | |
expectBody.isEqualTo<Nothing>(Person("42", "Ivan")) // compile but lead to "throw null" in bytecode | |
} | |
@Test | |
fun `test get 2`() { | |
val expectBody: BodySpec<Person, *> = client.get().uri("/person/27").exchange() | |
.expectBody(Person::class.java) | |
expectBody.consumeWith { person -> Assert.assertEquals("27", person.responseBody!!.id) } // doesn't compile here | |
expectBody.consumeWith<BodySpec<Person, *>> { person -> Assert.assertEquals("27", person.responseBody!!.id) } // doesn't compile here | |
expectBody.consumeWith<Nothing> { person -> Assert.assertEquals("27", person.responseBody!!.id) } // compile but lead to "throw null" in bytecode | |
} | |
@Test | |
fun `test get 3`() { | |
val bar: BodySpec<Person, *> = client.get().uri("/person/27").exchange() | |
.expectBody(Person::class.java).consumeWith { person -> Assert.assertTrue(true) } // compile but leads to NPE | |
} | |
@Test | |
fun `test list`() { | |
val expectBodyList: ListBodySpec<Person> = client.get().uri("/person").exchange() | |
.expectBodyList(Person::class.java) | |
expectBodyList.consumeWith<ListBodySpec<Person>> { list -> Assert.assertTrue(true) } // need to specify type param explicitly | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment