Skip to content

Commit d68b6bb

Browse files
committed
Polish "Add testing for AbstractFailureAnalyzer.findCause"
See gh-27862
1 parent a2eed67 commit d68b6bb

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@ public abstract class AbstractFailureAnalyzer<T extends Throwable> implements Fa
3131
@Override
3232
public FailureAnalysis analyze(Throwable failure) {
3333
T cause = findCause(failure, getCauseType());
34-
3534
return (cause != null) ? analyze(failure, cause) : null;
3635
}
3736

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/AbstractFailureAnalyzerTests.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,66 @@
1616

1717
package org.springframework.boot.diagnostics;
1818

19-
import org.junit.jupiter.api.BeforeEach;
19+
import java.io.IOException;
20+
2021
import org.junit.jupiter.api.Test;
2122

2223
import static org.assertj.core.api.Assertions.assertThat;
2324

25+
/**
26+
* Tests for {@link AbstractFailureAnalyzer}.
27+
*
28+
* @author Kim Jung Bin
29+
* @author Stephane Nicoll
30+
*/
2431
class AbstractFailureAnalyzerTests {
2532

26-
private FailureAnalyzerConcrete failureAnalyzerConcrete;
33+
private final TestFailureAnalyzer failureAnalyzer = new TestFailureAnalyzer();
2734

28-
@BeforeEach
29-
void configureFailureAnalyzer() {
30-
this.failureAnalyzerConcrete = new FailureAnalyzerConcrete();
35+
@Test
36+
void findCauseWithNullException() {
37+
assertThat(this.failureAnalyzer.findCause(null, Throwable.class)).isNull();
3138
}
3239

3340
@Test
34-
void findCauseExtendsThrowable() {
35-
ThrowableExtendsException ex = new ThrowableExtendsException();
36-
assertThat(this.failureAnalyzerConcrete.findCause(ex, Throwable.class).getClass()).isNotNull();
41+
void findCauseWithDirectExactMatch() {
42+
TestException ex = new TestException();
43+
assertThat(this.failureAnalyzer.findCause(ex, TestException.class)).isEqualTo(ex);
3744
}
3845

3946
@Test
40-
void findCauseExtendsOtherException() {
41-
ExtendsThrowableExtendsException ex = new ExtendsThrowableExtendsException();
42-
assertThat(this.failureAnalyzerConcrete.findCause(ex, ThrowableExtendsException.class).getClass()).isNotNull();
47+
void findCauseWithDirectSubClass() {
48+
SpecificTestException ex = new SpecificTestException();
49+
assertThat(this.failureAnalyzer.findCause(ex, TestException.class)).isEqualTo(ex);
4350
}
4451

4552
@Test
46-
void findCauseOtherException() {
47-
ThrowableExtendsException ex = new ThrowableExtendsException();
48-
assertThat(this.failureAnalyzerConcrete.findCause(ex, OtherException.class)).isNull();
53+
void findCauseWitNestedAndExactMatch() {
54+
TestException ex = new TestException();
55+
assertThat(this.failureAnalyzer.findCause(new IllegalArgumentException(new IllegalStateException(ex)),
56+
TestException.class)).isEqualTo(ex);
4957
}
5058

5159
@Test
52-
void findCauseNullObject() {
53-
assertThat(this.failureAnalyzerConcrete.findCause(null, Throwable.class)).isNull();
60+
void findCauseWitNestedAndSubClass() {
61+
SpecificTestException ex = new SpecificTestException();
62+
assertThat(this.failureAnalyzer.findCause(new IOException(new IllegalStateException(ex)), TestException.class))
63+
.isEqualTo(ex);
5464
}
5565

56-
static class FailureAnalyzerConcrete extends AbstractFailureAnalyzer<Throwable> {
66+
@Test
67+
void findCauseWithUnrelatedException() {
68+
IOException ex = new IOException();
69+
assertThat(this.failureAnalyzer.findCause(ex, TestException.class)).isNull();
70+
}
71+
72+
@Test
73+
void findCauseWithMoreSpecificException() {
74+
TestException ex = new TestException();
75+
assertThat(this.failureAnalyzer.findCause(ex, SpecificTestException.class)).isNull();
76+
}
77+
78+
static class TestFailureAnalyzer extends AbstractFailureAnalyzer<Throwable> {
5779

5880
@Override
5981
protected FailureAnalysis analyze(Throwable rootFailure, Throwable cause) {
@@ -62,15 +84,11 @@ protected FailureAnalysis analyze(Throwable rootFailure, Throwable cause) {
6284

6385
}
6486

65-
static class ThrowableExtendsException extends Throwable {
66-
67-
}
68-
69-
static class ExtendsThrowableExtendsException extends ThrowableExtendsException {
87+
static class TestException extends Exception {
7088

7189
}
7290

73-
static class OtherException extends Throwable {
91+
static class SpecificTestException extends TestException {
7492

7593
}
7694

0 commit comments

Comments
 (0)