Skip to content

Commit 5f98db6

Browse files
committed
Use new features introduced in JUnit Jupiter 5.11 and 5.12
See gh-34416
1 parent fb77fcd commit 5f98db6

File tree

6 files changed

+137
-9
lines changed

6 files changed

+137
-9
lines changed

Diff for: spring-aop/src/test/java/org/springframework/aop/framework/AbstractProxyExceptionHandlingTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.aopalliance.intercept.MethodInterceptor;
2323
import org.jspecify.annotations.Nullable;
2424
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.IndicativeSentencesGeneration;
2526
import org.junit.jupiter.api.Nested;
2627
import org.junit.jupiter.api.Test;
2728
import org.mockito.Mockito;
@@ -40,6 +41,7 @@
4041
* @see JdkProxyExceptionHandlingTests
4142
* @see CglibProxyExceptionHandlingTests
4243
*/
44+
@IndicativeSentencesGeneration(generator = SentenceFragmentDisplayNameGenerator.class)
4345
abstract class AbstractProxyExceptionHandlingTests {
4446

4547
private static final RuntimeException uncheckedException = new RuntimeException();
@@ -79,6 +81,7 @@ private static Answer<?> sneakyThrow(Throwable throwable) {
7981

8082

8183
@Nested
84+
@SentenceFragment("when there is one interceptor")
8285
class WhenThereIsOneInterceptorTests {
8386

8487
private @Nullable Throwable throwableSeenByInterceptor;
@@ -91,6 +94,7 @@ void beforeEach() {
9194
}
9295

9396
@Test
97+
@SentenceFragment("and the target throws an undeclared checked exception")
9498
void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
9599
willAnswer(sneakyThrow(undeclaredCheckedException)).given(target).doSomething();
96100
invokeProxy();
@@ -101,6 +105,7 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
101105
}
102106

103107
@Test
108+
@SentenceFragment("and the target throws a declared checked exception")
104109
void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException {
105110
willThrow(declaredCheckedException).given(target).doSomething();
106111
invokeProxy();
@@ -109,6 +114,7 @@ void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException {
109114
}
110115

111116
@Test
117+
@SentenceFragment("and the target throws an unchecked exception")
112118
void targetThrowsUncheckedException() throws DeclaredCheckedException {
113119
willThrow(uncheckedException).given(target).doSomething();
114120
invokeProxy();
@@ -131,6 +137,7 @@ private MethodInterceptor captureThrowable() {
131137

132138

133139
@Nested
140+
@SentenceFragment("when there are no interceptors")
134141
class WhenThereAreNoInterceptorsTests {
135142

136143
@BeforeEach
@@ -140,6 +147,7 @@ void beforeEach() {
140147
}
141148

142149
@Test
150+
@SentenceFragment("and the target throws an undeclared checked exception")
143151
void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
144152
willAnswer(sneakyThrow(undeclaredCheckedException)).given(target).doSomething();
145153
invokeProxy();
@@ -149,13 +157,15 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
149157
}
150158

151159
@Test
160+
@SentenceFragment("and the target throws a declared checked exception")
152161
void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException {
153162
willThrow(declaredCheckedException).given(target).doSomething();
154163
invokeProxy();
155164
assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException);
156165
}
157166

158167
@Test
168+
@SentenceFragment("and the target throws an unchecked exception")
159169
void targetThrowsUncheckedException() throws DeclaredCheckedException {
160170
willThrow(uncheckedException).given(target).doSomething();
161171
invokeProxy();

Diff for: spring-aop/src/test/java/org/springframework/aop/framework/CglibProxyExceptionHandlingTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.aop.framework;
1818

1919
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.DisplayName;
2021

2122
import org.springframework.cglib.proxy.Enhancer;
2223

@@ -27,6 +28,7 @@
2728
* @since 6.2
2829
* @see JdkProxyExceptionHandlingTests
2930
*/
31+
@DisplayName("CGLIB proxy exception handling")
3032
class CglibProxyExceptionHandlingTests extends AbstractProxyExceptionHandlingTests {
3133

3234
@BeforeEach

Diff for: spring-aop/src/test/java/org/springframework/aop/framework/JdkProxyExceptionHandlingTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -18,13 +18,16 @@
1818

1919
import java.lang.reflect.Proxy;
2020

21+
import org.junit.jupiter.api.DisplayName;
22+
2123
import static org.assertj.core.api.Assertions.assertThat;
2224

2325
/**
2426
* @author Mikaël Francoeur
2527
* @since 6.2
2628
* @see CglibProxyExceptionHandlingTests
2729
*/
30+
@DisplayName("JDK proxy exception handling")
2831
class JdkProxyExceptionHandlingTests extends AbstractProxyExceptionHandlingTests {
2932

3033
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2025 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.aop.framework;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* {@code @SentenceFragment} is used to configure a sentence fragment for use
26+
* with JUnit Jupiter's
27+
* {@link org.junit.jupiter.api.DisplayNameGenerator.IndicativeSentences}
28+
* {@code DisplayNameGenerator}.
29+
*
30+
* @author Sam Brannen
31+
* @since 7.0
32+
* @see SentenceFragmentDisplayNameGenerator
33+
* @see org.junit.jupiter.api.DisplayName
34+
*/
35+
@Target({ ElementType.TYPE, ElementType.METHOD })
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@interface SentenceFragment {
38+
39+
String value();
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2002-2025 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.aop.framework;
18+
19+
import java.lang.reflect.AnnotatedElement;
20+
import java.lang.reflect.Method;
21+
import java.util.List;
22+
23+
import org.junit.platform.commons.support.AnnotationSupport;
24+
import org.junit.platform.commons.util.StringUtils;
25+
26+
/**
27+
* Extension of {@link org.junit.jupiter.api.DisplayNameGenerator.Simple} that
28+
* supports custom sentence fragments configured via
29+
* {@link SentenceFragment @SentenceFragment}.
30+
*
31+
* <p>This generator can be configured for use with JUnit Jupiter's
32+
* {@link org.junit.jupiter.api.DisplayNameGenerator.IndicativeSentences
33+
* IndicativeSentences} {@code DisplayNameGenerator} via the
34+
* {@link org.junit.jupiter.api.IndicativeSentencesGeneration#generator generator}
35+
* attribute in {@code @IndicativeSentencesGeneration}.
36+
*
37+
* @author Sam Brannen
38+
* @since 7.0
39+
* @see SentenceFragment @SentenceFragment
40+
*/
41+
class SentenceFragmentDisplayNameGenerator extends org.junit.jupiter.api.DisplayNameGenerator.Simple {
42+
43+
@Override
44+
public String generateDisplayNameForClass(Class<?> testClass) {
45+
String sentenceFragment = getSentenceFragment(testClass);
46+
return (sentenceFragment != null ? sentenceFragment :
47+
super.generateDisplayNameForClass(testClass));
48+
}
49+
50+
@Override
51+
public String generateDisplayNameForNestedClass(List<Class<?>> enclosingInstanceTypes,
52+
Class<?> nestedClass) {
53+
54+
String sentenceFragment = getSentenceFragment(nestedClass);
55+
return (sentenceFragment != null ? sentenceFragment :
56+
super.generateDisplayNameForNestedClass(enclosingInstanceTypes, nestedClass));
57+
}
58+
59+
@Override
60+
public String generateDisplayNameForMethod(List<Class<?>> enclosingInstanceTypes,
61+
Class<?> testClass, Method testMethod) {
62+
63+
String sentenceFragment = getSentenceFragment(testMethod);
64+
return (sentenceFragment != null ? sentenceFragment :
65+
super.generateDisplayNameForMethod(enclosingInstanceTypes, testClass, testMethod));
66+
}
67+
68+
private static final String getSentenceFragment(AnnotatedElement element) {
69+
return AnnotationSupport.findAnnotation(element, SentenceFragment.class)
70+
.map(SentenceFragment::value)
71+
.map(String::trim)
72+
.filter(StringUtils::isNotBlank)
73+
.orElse(null);
74+
}
75+
76+
}

Diff for: spring-context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -33,7 +33,7 @@
3333
import java.util.concurrent.TimeUnit;
3434

3535
import org.assertj.core.api.AbstractAssert;
36-
import org.junit.jupiter.api.AfterEach;
36+
import org.junit.jupiter.api.AutoClose;
3737
import org.junit.jupiter.api.Test;
3838
import org.junit.jupiter.api.extension.ParameterContext;
3939
import org.junit.jupiter.params.ParameterizedTest;
@@ -84,14 +84,10 @@
8484
*/
8585
class ScheduledAnnotationBeanPostProcessorTests {
8686

87+
@AutoClose
8788
private final StaticApplicationContext context = new StaticApplicationContext();
8889

8990

90-
@AfterEach
91-
void closeContextAfterTest() {
92-
context.close();
93-
}
94-
9591
@ParameterizedTest
9692
@CsvSource(textBlock = """
9793
FixedDelay, 5_000

0 commit comments

Comments
 (0)