Skip to content

Commit c6ecaac

Browse files
committed
Add getResult/hasResult methods to DeferredResult
Issue: SPR-10603
1 parent 9bc4663 commit c6ecaac

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ public final boolean isSetOrExpired() {
107107
return ((this.result != RESULT_NONE) || this.expired);
108108
}
109109

110+
/**
111+
* @return {@code true} if the DeferredResult has been set.
112+
*/
113+
public boolean hasResult() {
114+
return this.result != RESULT_NONE;
115+
}
116+
117+
/**
118+
* @return the result or {@code null} if the result wasn't set; since the result can
119+
* also be {@code null}, it is recommended to use {@link #hasResult()} first
120+
* to check if there is a result prior to calling this method.
121+
*/
122+
public Object getResult() {
123+
return hasResult() ? this.result : null;
124+
}
125+
110126
/**
111127
* Return the configured timeout value in milliseconds.
112128
*/

spring-web/src/test/java/org/springframework/web/context/request/async/DeferredResultTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler;
2121

2222
import static org.junit.Assert.*;
23-
import static org.mockito.BDDMockito.*;
23+
import static org.mockito.Mockito.*;
2424

2525
/**
2626
* DeferredResult tests.
@@ -69,6 +69,21 @@ public void isSetOrExpired() {
6969
verify(handler).handleResult("hello");
7070
}
7171

72+
@Test
73+
public void hasResult() {
74+
DeferredResultHandler handler = mock(DeferredResultHandler.class);
75+
76+
DeferredResult<String> result = new DeferredResult<String>();
77+
result.setResultHandler(handler);
78+
79+
assertFalse(result.hasResult());
80+
assertNull(result.getResult());
81+
82+
result.setResult("hello");
83+
84+
assertEquals("hello", result.getResult());
85+
}
86+
7287
@Test
7388
public void onCompletion() throws Exception {
7489
final StringBuilder sb = new StringBuilder();

0 commit comments

Comments
 (0)