Skip to content

Commit a2eed67

Browse files
smoothbearsnicoll
authored andcommitted
Add testing for AbstractFailureAnalyzer.findCause
See gh-27862
1 parent d06ebce commit a2eed67

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/AbstractFailureAnalyzer.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ public abstract class AbstractFailureAnalyzer<T extends Throwable> implements Fa
3131
@Override
3232
public FailureAnalysis analyze(Throwable failure) {
3333
T cause = findCause(failure, getCauseType());
34-
if (cause != null) {
35-
return analyze(failure, cause);
36-
}
37-
return null;
34+
35+
return (cause != null) ? analyze(failure, cause) : null;
3836
}
3937

4038
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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+
17+
package org.springframework.boot.diagnostics;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
class AbstractFailureAnalyzerTests {
25+
26+
private FailureAnalyzerConcrete failureAnalyzerConcrete;
27+
28+
@BeforeEach
29+
void configureFailureAnalyzer() {
30+
this.failureAnalyzerConcrete = new FailureAnalyzerConcrete();
31+
}
32+
33+
@Test
34+
void findCauseExtendsThrowable() {
35+
ThrowableExtendsException ex = new ThrowableExtendsException();
36+
assertThat(this.failureAnalyzerConcrete.findCause(ex, Throwable.class).getClass()).isNotNull();
37+
}
38+
39+
@Test
40+
void findCauseExtendsOtherException() {
41+
ExtendsThrowableExtendsException ex = new ExtendsThrowableExtendsException();
42+
assertThat(this.failureAnalyzerConcrete.findCause(ex, ThrowableExtendsException.class).getClass()).isNotNull();
43+
}
44+
45+
@Test
46+
void findCauseOtherException() {
47+
ThrowableExtendsException ex = new ThrowableExtendsException();
48+
assertThat(this.failureAnalyzerConcrete.findCause(ex, OtherException.class)).isNull();
49+
}
50+
51+
@Test
52+
void findCauseNullObject() {
53+
assertThat(this.failureAnalyzerConcrete.findCause(null, Throwable.class)).isNull();
54+
}
55+
56+
static class FailureAnalyzerConcrete extends AbstractFailureAnalyzer<Throwable> {
57+
58+
@Override
59+
protected FailureAnalysis analyze(Throwable rootFailure, Throwable cause) {
60+
return null;
61+
}
62+
63+
}
64+
65+
static class ThrowableExtendsException extends Throwable {
66+
67+
}
68+
69+
static class ExtendsThrowableExtendsException extends ThrowableExtendsException {
70+
71+
}
72+
73+
static class OtherException extends Throwable {
74+
75+
}
76+
77+
}

0 commit comments

Comments
 (0)