Skip to content

Commit 18e411d

Browse files
artembilanspring-builds
authored andcommitted
More @DirtiesContext for tests in core
An attempt to mitigate a memory impact on limited CI/CD runners (cherry picked from commit b5ec098)
1 parent ec2cfeb commit 18e411d

24 files changed

+126
-171
lines changed

spring-integration-core/src/test/java/org/springframework/integration/config/xml/ChannelAutoCreationTests.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,53 @@
1616

1717
package org.springframework.integration.config.xml;
1818

19-
import org.junit.Test;
19+
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.beans.factory.BeanCreationException;
2222
import org.springframework.context.support.ClassPathXmlApplicationContext;
2323

24+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25+
import static org.assertj.core.api.Assertions.assertThatNoException;
26+
2427
/**
2528
*
2629
* @author Oleg Zhurakousky
2730
* @author Gary Russell
31+
* @author Artem Bilan
2832
*
2933
*/
3034
public class ChannelAutoCreationTests {
3135

32-
@Test // no assertions since it validates that no exception is thrown
36+
@Test
3337
public void testEnablingAutoChannelCreationBeforeWithCustom() {
34-
new ClassPathXmlApplicationContext("TestEnableChannelAutoCreation-before-context.xml", this.getClass()).close();
38+
assertThatNoException()
39+
.isThrownBy(() ->
40+
new ClassPathXmlApplicationContext(
41+
"TestEnableChannelAutoCreation-before-context.xml", this.getClass()));
3542
}
3643

37-
@Test // no assertions since it validates that no exception is thrown
44+
@Test
3845
public void testEnablingAutoChannelCreationAfterWithCustom() {
39-
new ClassPathXmlApplicationContext("TestEnableChannelAutoCreation-after-context.xml", this.getClass()).close();
46+
assertThatNoException()
47+
.isThrownBy(() ->
48+
new ClassPathXmlApplicationContext(
49+
"TestEnableChannelAutoCreation-after-context.xml", this.getClass()));
4050
}
4151

42-
@Test(expected = BeanCreationException.class)
52+
@Test
4353
public void testDisablingAutoChannelCreationAfter() {
44-
new ClassPathXmlApplicationContext("TestDisableChannelAutoCreation-after-context.xml", this.getClass()).close();
54+
assertThatExceptionOfType(BeanCreationException.class)
55+
.isThrownBy(() ->
56+
new ClassPathXmlApplicationContext(
57+
"TestDisableChannelAutoCreation-after-context.xml", getClass()));
4558
}
4659

47-
@Test(expected = BeanCreationException.class)
60+
@Test
4861
public void testDisablingAutoChannelCreationBefore() {
49-
new ClassPathXmlApplicationContext("TestDisableChannelAutoCreation-before-context.xml", this.getClass())
50-
.close();
62+
assertThatExceptionOfType(BeanCreationException.class)
63+
.isThrownBy(() ->
64+
new ClassPathXmlApplicationContext(
65+
"TestDisableChannelAutoCreation-before-context.xml", this.getClass()));
5166
}
5267

5368
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -16,14 +16,13 @@
1616

1717
package org.springframework.integration.config.xml;
1818

19-
import org.junit.Test;
20-
import org.junit.runner.RunWith;
19+
import org.junit.jupiter.api.Test;
2120

2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.context.ApplicationContext;
2423
import org.springframework.integration.test.util.TestUtils;
25-
import org.springframework.test.context.ContextConfiguration;
26-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24+
import org.springframework.test.annotation.DirtiesContext;
25+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2726

2827
import static org.assertj.core.api.Assertions.assertThat;
2928

@@ -34,22 +33,20 @@
3433
*
3534
* @since 2.1
3635
*/
37-
@RunWith(SpringJUnit4ClassRunner.class)
38-
@ContextConfiguration
39-
public class EnricherParserTests2 {
36+
@SpringJUnitConfig
37+
@DirtiesContext
38+
public class EnricherParser2Tests {
4039

4140
@Autowired
4241
private ApplicationContext context;
4342

4443
@Test
4544
public void configurationCheckRequiresReply() {
46-
4745
Object endpoint = context.getBean("enricher");
4846

4947
boolean requiresReply = TestUtils.getPropertyValue(endpoint, "handler.requiresReply", Boolean.class);
5048

5149
assertThat(requiresReply).as("Was expecting requiresReply to be 'false'").isFalse();
52-
5350
}
5451

5552
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<property name="nested.value" expression="@someBean.someOtherProperty" />
1818
</enricher>
1919

20-
<beans:bean id="someBean" class="org.springframework.integration.config.xml.EnricherParserTests3$SomeBean">
20+
<beans:bean id="someBean" class="org.springframework.integration.config.xml.EnricherParser3Tests$SomeBean">
2121
<beans:constructor-arg value="baz" />
2222
</beans:bean>
2323

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<property name="@someBean.nested.value" value="qux" />
1818
</enricher>
1919

20-
<beans:bean id="someBean" class="org.springframework.integration.config.xml.EnricherParserTests3$SomeBean">
20+
<beans:bean id="someBean" class="org.springframework.integration.config.xml.EnricherParser3Tests$SomeBean">
2121
<beans:constructor-arg value="baz" />
2222
</beans:bean>
2323

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
import org.springframework.messaging.support.GenericMessage;
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
30-
import static org.assertj.core.api.Assertions.fail;
30+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3131

3232
/**
3333
* @author Gary Russell
3434
*
3535
* @since 2.1.1
3636
*/
37-
public class EnricherParserTests3 {
37+
public class EnricherParser3Tests {
3838

3939
@Test
4040
public void testSourceBeanResolver() {
@@ -44,7 +44,7 @@ public void testSourceBeanResolver() {
4444
PollableChannel beanResolveOut = context.getBean("beanResolveOut", PollableChannel.class);
4545
SomeBean payload = new SomeBean("foo");
4646
assertThat(payload.getNested().getValue()).isEqualTo("foo");
47-
beanResolveIn.send(new GenericMessage<SomeBean>(payload));
47+
beanResolveIn.send(new GenericMessage<>(payload));
4848
@SuppressWarnings("unchecked")
4949
Message<SomeBean> out = (Message<SomeBean>) beanResolveOut.receive();
5050
assertThat(out.getPayload()).isSameAs(payload);
@@ -59,13 +59,9 @@ public void testTargetBeanResolver() {
5959
MessageChannel beanResolveIn = context.getBean("beanResolveIn", MessageChannel.class);
6060
SomeBean payload = new SomeBean("foo");
6161
assertThat(payload.getNested().getValue()).isEqualTo("foo");
62-
try {
63-
beanResolveIn.send(new GenericMessage<SomeBean>(payload));
64-
fail("Expected SpEL Exception");
65-
}
66-
catch (MessageHandlingException e) {
67-
assertThat(e.getCause() instanceof SpelEvaluationException).isTrue();
68-
}
62+
assertThatExceptionOfType(MessageHandlingException.class)
63+
.isThrownBy(() -> beanResolveIn.send(new GenericMessage<>(payload)))
64+
.withCauseInstanceOf(SpelEvaluationException.class);
6965
context.close();
7066
}
7167

@@ -85,7 +81,7 @@ public String getSomeOtherProperty() {
8581
return "bar";
8682
}
8783

88-
public class Nested {
84+
public static class Nested {
8985

9086
private String value;
9187

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
null-result-expression="'Could not determine the notOverwrite'"/>
4141

4242
<request-handler-advice-chain>
43-
<beans:bean class="org.springframework.integration.config.xml.EnricherParserTests4$FooAdvice" />
43+
<beans:bean class="org.springframework.integration.config.xml.EnricherParser4Tests$FooAdvice" />
4444
</request-handler-advice-chain>
4545
</enricher>
4646

47-
<util:constant id="testBean" static-field="org.springframework.integration.config.xml.EnricherParserTests4$Gender.MALE"/>
47+
<util:constant id="testBean" static-field="org.springframework.integration.config.xml.EnricherParser4Tests$Gender.MALE"/>
4848

4949
</beans:beans>
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.integration.config.xml;
1818

19-
import org.junit.Test;
20-
import org.junit.runner.RunWith;
19+
import org.junit.jupiter.api.Test;
2120

2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.context.ApplicationContext;
@@ -29,19 +28,20 @@
2928
import org.springframework.messaging.MessageHeaders;
3029
import org.springframework.messaging.PollableChannel;
3130
import org.springframework.messaging.SubscribableChannel;
32-
import org.springframework.test.context.ContextConfiguration;
33-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31+
import org.springframework.test.annotation.DirtiesContext;
32+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3433

3534
import static org.assertj.core.api.Assertions.assertThat;
3635

3736
/**
3837
* @author Liujiong
38+
* @author Artem Bilan
3939
*
4040
* @since 4.1
4141
*/
42-
@RunWith(SpringJUnit4ClassRunner.class)
43-
@ContextConfiguration
44-
public class EnricherParserTests4 {
42+
@SpringJUnitConfig
43+
@DirtiesContext
44+
public class EnricherParser4Tests {
4545

4646
@Autowired
4747
private ApplicationContext context;
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.integration.config.xml;
1818

19-
import org.junit.Test;
20-
import org.junit.runner.RunWith;
19+
import org.junit.jupiter.api.Test;
2120

2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.context.ApplicationContext;
@@ -26,8 +25,8 @@
2625
import org.springframework.integration.support.MessageBuilder;
2726
import org.springframework.messaging.Message;
2827
import org.springframework.messaging.PollableChannel;
29-
import org.springframework.test.context.ContextConfiguration;
30-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28+
import org.springframework.test.annotation.DirtiesContext;
29+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3130

3231
import static org.assertj.core.api.Assertions.assertThat;
3332

@@ -36,11 +35,13 @@
3635
* a default object in case of downstream failure.
3736
*
3837
* @author Kris Jacyna
38+
* @author Artem Bilan
39+
*
3940
* @since 4.1
4041
*/
41-
@RunWith(SpringJUnit4ClassRunner.class)
42-
@ContextConfiguration
43-
public class EnricherParserTests5 {
42+
@SpringJUnitConfig
43+
@DirtiesContext
44+
public class EnricherParser5Tests {
4445

4546
@Autowired
4647
private ApplicationContext context;

0 commit comments

Comments
 (0)