Add ResponseEntity.ofNullable()

To deal with non-Optional nullable objects.

Closes gh-29117
This commit is contained in:
Sébastien Deleuze
2023-02-02 16:39:05 +01:00
parent 7440afe571
commit f7418704de
3 changed files with 84 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Arjen Poutsma
* @author Marcel Overdijk
* @author Kazuki Shimizu
* @author Sebastien Deleuze
*/
class ResponseEntityTests {
@@ -90,6 +91,25 @@ class ResponseEntityTests {
assertThat(responseEntity.getBody()).isNull();
}
@Test
void ofNullable() {
Integer entity = 42;
ResponseEntity<Integer> responseEntity = ResponseEntity.ofNullable(entity);
assertThat(responseEntity).isNotNull();
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat((int) responseEntity.getBody()).isEqualTo((int) entity);
}
@Test
void ofNullNullable() {
ResponseEntity<Integer> responseEntity = ResponseEntity.ofNullable(null);
assertThat(responseEntity).isNotNull();
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(responseEntity.getBody()).isNull();
}
@Test
void createdLocation() {
URI location = URI.create("location");