Skip to content

Commit be8c1e2

Browse files
committed
Optimize SpringIntegrationTestExecutionListener
There is no need to request `mockIntegrationContext.getAutoStartupCandidates()` one more time from the `afterTestClass()` if we can easily extract that into property from the `prepareTestInstance()`
1 parent 3d16e59 commit be8c1e2

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTestExecutionListener.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2024 the original author or authors.
2+
* Copyright 2017-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.integration.test.context;
1818

1919
import java.util.Arrays;
20+
import java.util.List;
2021

2122
import org.springframework.context.ApplicationContext;
2223
import org.springframework.integration.endpoint.AbstractEndpoint;
@@ -36,6 +37,8 @@
3637
*/
3738
class SpringIntegrationTestExecutionListener implements TestExecutionListener {
3839

40+
private List<AbstractEndpoint> autoStartupCandidates;
41+
3942
@Override
4043
public void prepareTestInstance(TestContext testContext) {
4144
SpringIntegrationTest springIntegrationTest =
@@ -45,7 +48,8 @@ public void prepareTestInstance(TestContext testContext) {
4548

4649
ApplicationContext applicationContext = testContext.getApplicationContext();
4750
MockIntegrationContext mockIntegrationContext = applicationContext.getBean(MockIntegrationContext.class);
48-
mockIntegrationContext.getAutoStartupCandidates()
51+
this.autoStartupCandidates = mockIntegrationContext.getAutoStartupCandidates();
52+
this.autoStartupCandidates
4953
.stream()
5054
.filter(endpoint -> !match(endpoint.getBeanName(), patterns))
5155
.peek(endpoint -> endpoint.setAutoStartup(true))
@@ -54,13 +58,10 @@ public void prepareTestInstance(TestContext testContext) {
5458

5559
@Override
5660
public void afterTestClass(TestContext testContext) {
57-
ApplicationContext applicationContext = testContext.getApplicationContext();
58-
MockIntegrationContext mockIntegrationContext = applicationContext.getBean(MockIntegrationContext.class);
59-
mockIntegrationContext.getAutoStartupCandidates()
60-
.forEach(AbstractEndpoint::stop);
61+
this.autoStartupCandidates.forEach(AbstractEndpoint::stop);
6162
}
6263

63-
private boolean match(String name, String[] patterns) {
64+
private static boolean match(String name, String[] patterns) {
6465
return patterns.length > 0 &&
6566
Arrays.stream(patterns)
6667
.anyMatch(pattern -> PatternMatchUtils.simpleMatch(pattern, name));

0 commit comments

Comments
 (0)