Add ResponseEntity.ofNullable()
To deal with non-Optional nullable objects. Closes gh-29117
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user