8000 Step 5: Error handling · blackbeltcoder/reactive-spring@1e5e6f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e5e6f3

Browse files
committed
Step 5: Error handling
1 parent f0416c0 commit 1e5e6f3

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
32BD
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package workshop;
17+
18+
import reactor.core.publisher.Mono;
19+
import reactor.test.StepVerifier;
20+
21+
import org.junit.Test;
22+
23+
/**
24+
* Tests to handle error cases.
25+
*
26+
* @author Mark Paluch
27+
*/
28+
public class Step5ErrorHandling {
29+
30+
@Test
31+
public void monoShouldEmitAFallbackValueOnError() {
32+
33+
Mono<String> failedMono = Mono.error(new IllegalStateException("Something bad happened!"));
34+
35+
String recoveryValue = "Recovered!";
36+
37+
// TODO: Replace this line with a fallback value in an error happens
38+
Mono<String> withErrorHandling = failedMono;
39+
40+
StepVerifier.create(withErrorHandling).expectNext("Recovered!").verifyComplete();
41+
}
42+
43+
@Test
44+
public void monoShouldEmitADeferredFallbackValueOnError() {
45+
46+
Mono<String> failedMono = Mono.error(new IllegalStateException("Something bad happened!"));
47+
48+
Mono<String> recoveryValue = Mono.just("Recovered!");
49+
50+
// TODO: Replace this line with a fallback that is computed in a deferred way
51+
Mono<String> withErrorHandling = failedMono;
52+
53+
StepVerifier.create(withErrorHandling).expectNext("Recovered!").verifyComplete();
54+
}
55+
56+
@Test
57+
public void monoShouldEmitAFallbackValueIfEmpty() {
58+
59+
Mono<String> emptyMono = Mono.empty();
60+
61+
Mono<String> recoveryValue = Mono.just("Recovered!");
62+
63+
// TODO: Replace this line with a fallback that is computed in a deferred way
64+
Mono<String> withErrorHandling = emptyMono;
65+
66+
StepVerifier.create(withErrorHandling).expectNext("Recovered!").verifyComplete();
67+
}
68+
69+
@Test
70+
public void monoShouldTranslateException() {
71+
72+
Mono<String> failedMono = Mono.error(new IllegalStateException("Something bad happened!"));
73+
74+
// TODO: Replace this line with a Mono translating IllegalStateException to MyBusinessException
75+
Mono<String> withErrorHandling = failedMono;
76+
77+
StepVerifier.create(withErrorHandling).expectError(MyBusinessException.class).verify();
78+
}
79+
80+
static class MyBusinessException extends Exception {
81+
82+
public MyBusinessException(Throwable cause) {
83+
super(cause);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)
0