Skip to content

Commit 59860c0

Browse files
artembilangaryrussell
authored andcommitted
INT-4535: Fix @SpringIntegrationTest for caching
JIRA: https://jira.spring.io/browse/INT-4535 For the proper Spring Test Context caching support, the `ContextCustomizer` must implement `hashCode()` and `equals()` methods * Add `hashCode()` and `equals()` into the `MockIntegrationContextCustomizer` * Ensure that caching works as excepted with the `CachedSpringIntegrationTestAnnotationTests` its two sub-classes **Cherry-pick to 5.0.x**
1 parent 4ee5294 commit 59860c0

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -54,12 +54,28 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
5454
new RootBeanDefinition(MockIntegrationContext.class));
5555

5656
String endpointsInitializer = Introspector.decapitalize(IntegrationEndpointsInitializer.class.getSimpleName());
57+
5758
registry.registerBeanDefinition(endpointsInitializer,
5859
BeanDefinitionBuilder.genericBeanDefinition(IntegrationEndpointsInitializer.class)
5960
.addConstructorArgValue(this.springIntegrationTest)
6061
.getBeanDefinition());
6162

6263
}
6364

65+
@Override
66+
public int hashCode() {
67+
return this.springIntegrationTest.hashCode();
68+
}
69+
70+
@Override
71+
public boolean equals(Object obj) {
72+
if (obj == null || obj.getClass() != getClass()) {
73+
return false;
74+
}
75+
76+
MockIntegrationContextCustomizer customizer = (MockIntegrationContextCustomizer) obj;
77+
return this.springIntegrationTest.equals(customizer.springIntegrationTest);
78+
}
79+
6480
}
6581

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -37,11 +37,12 @@ class MockIntegrationContextCustomizerFactory implements ContextCustomizerFactor
3737
@Override
3838
public ContextCustomizer createContextCustomizer(Class<?> testClass,
3939
List<ContextConfigurationAttributes> configAttributes) {
40+
4041
SpringIntegrationTest springIntegrationTest =
4142
AnnotatedElementUtils.findMergedAnnotation(testClass, SpringIntegrationTest.class);
43+
4244
return springIntegrationTest != null
43-
?
44-
new MockIntegrationContextCustomizer(springIntegrationTest)
45+
? new MockIntegrationContextCustomizer(springIntegrationTest)
4546
: null;
4647
}
4748

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2018 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+
17+
package org.springframework.integration.test.mock;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.integration.test.context.SpringIntegrationTest;
27+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
28+
29+
/**
30+
* @author Artem Bilan
31+
*
32+
* @since 5.0.9
33+
*/
34+
@SpringJUnitConfig
35+
@SpringIntegrationTest
36+
public abstract class CachedSpringIntegrationTestAnnotationTests {
37+
38+
private static int beanFactoryPostProcessorCallCounter;
39+
40+
public static class SpringIntegrationTestAnnotationCaching1Tests
41+
extends CachedSpringIntegrationTestAnnotationTests {
42+
43+
@Test
44+
public void testSingleApplicationContext() {
45+
assertThat(beanFactoryPostProcessorCallCounter).isEqualTo(1);
46+
}
47+
48+
}
49+
50+
public static class SpringIntegrationTestAnnotationCaching2Tests
51+
extends CachedSpringIntegrationTestAnnotationTests {
52+
53+
@Test
54+
public void testSingleApplicationContext() {
55+
assertThat(beanFactoryPostProcessorCallCounter).isEqualTo(1);
56+
}
57+
58+
}
59+
60+
@Configuration
61+
public static class ContextConfiguration {
62+
63+
@Bean
64+
public static BeanFactoryPostProcessor tesBeanFactoryPostProcessor() {
65+
return beanFactory -> beanFactoryPostProcessorCallCounter++;
66+
}
67+
68+
}
69+
70+
}

spring-integration-test/src/test/java/org/springframework/integration/test/mock/MockMessageHandlerTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.springframework.messaging.PollableChannel;
6363
import org.springframework.messaging.SubscribableChannel;
6464
import org.springframework.messaging.support.GenericMessage;
65+
import org.springframework.test.annotation.DirtiesContext;
6566
import org.springframework.test.context.ContextConfiguration;
6667
import org.springframework.test.context.junit4.SpringRunner;
6768

@@ -74,6 +75,7 @@
7475
@RunWith(SpringRunner.class)
7576
@ContextConfiguration(classes = MockMessageHandlerTests.Config.class)
7677
@SpringIntegrationTest
78+
@DirtiesContext
7779
public class MockMessageHandlerTests {
7880

7981
@Autowired

0 commit comments

Comments
 (0)