Skip to content

Commit bf4ffac

Browse files
committed
Stop relying on broker's auto-configuration
This commit isolates JmsAutoConfiguration tests so that they do not rely on an actual broker. Some tests that are no longer relevant have been adapted as well. Closes gh-44734
1 parent 15efbd6 commit bf4ffac

File tree

1 file changed

+19
-52
lines changed

1 file changed

+19
-52
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java

+19-52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-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.
@@ -16,20 +16,16 @@
1616

1717
package org.springframework.boot.autoconfigure.jms;
1818

19-
import java.io.IOException;
20-
2119
import io.micrometer.observation.ObservationRegistry;
2220
import jakarta.jms.ConnectionFactory;
2321
import jakarta.jms.ExceptionListener;
2422
import jakarta.jms.Session;
25-
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
2623
import org.junit.jupiter.api.Test;
2724

2825
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
2926
import org.springframework.aot.test.generate.TestGenerationContext;
3027
import org.springframework.beans.factory.config.BeanPostProcessor;
3128
import org.springframework.boot.autoconfigure.AutoConfigurations;
32-
import org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration;
3329
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3430
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3531
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -45,7 +41,6 @@
4541
import org.springframework.jms.config.JmsListenerEndpoint;
4642
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
4743
import org.springframework.jms.config.SimpleJmsListenerEndpoint;
48-
import org.springframework.jms.connection.CachingConnectionFactory;
4944
import org.springframework.jms.core.JmsMessagingTemplate;
5045
import org.springframework.jms.core.JmsTemplate;
5146
import org.springframework.jms.listener.DefaultMessageListenerContainer;
@@ -69,31 +64,30 @@
6964
class JmsAutoConfigurationTests {
7065

7166
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
72-
.withConfiguration(AutoConfigurations.of(ArtemisAutoConfiguration.class, JmsAutoConfiguration.class));
67+
.withBean(ConnectionFactory.class, () -> mock(ConnectionFactory.class))
68+
.withConfiguration(AutoConfigurations.of(JmsAutoConfiguration.class));
69+
70+
@Test
71+
void testNoConnectionFactoryJmsConfiguration() {
72+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(JmsAutoConfiguration.class))
73+
.run((context) -> assertThat(context).doesNotHaveBean(JmsTemplate.class)
74+
.doesNotHaveBean(JmsMessagingTemplate.class)
75+
.doesNotHaveBean(DefaultJmsListenerContainerFactoryConfigurer.class)
76+
.doesNotHaveBean(DefaultJmsListenerContainerFactory.class));
77+
}
7378

7479
@Test
7580
void testDefaultJmsConfiguration() {
7681
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
77-
assertThat(context).hasSingleBean(ConnectionFactory.class);
78-
assertThat(context).hasSingleBean(CachingConnectionFactory.class);
79-
CachingConnectionFactory factory = context.getBean(CachingConnectionFactory.class);
80-
assertThat(factory.getTargetConnectionFactory()).isInstanceOf(ActiveMQConnectionFactory.class);
82+
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
8183
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
8284
JmsMessagingTemplate messagingTemplate = context.getBean(JmsMessagingTemplate.class);
83-
assertThat(factory).isEqualTo(jmsTemplate.getConnectionFactory());
85+
assertThat(jmsTemplate.getConnectionFactory()).isEqualTo(connectionFactory);
8486
assertThat(messagingTemplate.getJmsTemplate()).isEqualTo(jmsTemplate);
85-
assertThat(getBrokerUrl(factory)).startsWith("vm://");
8687
assertThat(context.containsBean("jmsListenerContainerFactory")).isTrue();
8788
});
8889
}
8990

90-
@Test
91-
void testConnectionFactoryBackOff() {
92-
this.contextRunner.withUserConfiguration(TestConfiguration2.class)
93-
.run((context) -> assertThat(context.getBeansOfType(ActiveMQConnectionFactory.class))
94-
.containsOnlyKeys("customConnectionFactory"));
95-
}
96-
9791
@Test
9892
void testJmsTemplateBackOff() {
9993
this.contextRunner.withUserConfiguration(TestConfiguration3.class)
@@ -107,27 +101,10 @@ void testJmsMessagingTemplateBackOff() {
107101
.isEqualTo("fooBar"));
108102
}
109103

110-
@Test
111-
void testJmsTemplateBackOffEverything() {
112-
this.contextRunner
113-
.withUserConfiguration(TestConfiguration2.class, TestConfiguration3.class, TestConfiguration5.class)
114-
.run(this::testJmsTemplateBackOffEverything);
115-
}
116-
117-
private void testJmsTemplateBackOffEverything(AssertableApplicationContext loaded) {
118-
JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class);
119-
assertThat(jmsTemplate.getPriority()).isEqualTo(999);
120-
assertThat(loaded.getBeansOfType(ActiveMQConnectionFactory.class)).containsOnlyKeys("customConnectionFactory");
121-
JmsMessagingTemplate messagingTemplate = loaded.getBean(JmsMessagingTemplate.class);
122-
assertThat(messagingTemplate.getDefaultDestinationName()).isEqualTo("fooBar");
123-
assertThat(messagingTemplate.getJmsTemplate()).isEqualTo(jmsTemplate);
124-
}
125-
126104
@Test
127105
void testDefaultJmsListenerConfiguration() {
128106
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((loaded) -> {
129-
assertThat(loaded).hasSingleBean(CachingConnectionFactory.class);
130-
CachingConnectionFactory connectionFactory = loaded.getBean(CachingConnectionFactory.class);
107+
ConnectionFactory connectionFactory = loaded.getBean(ConnectionFactory.class);
131108
assertThat(loaded).hasSingleBean(DefaultJmsListenerContainerFactory.class);
132109
DefaultJmsListenerContainerFactory containerFactory = loaded
133110
.getBean(DefaultJmsListenerContainerFactory.class);
@@ -423,16 +400,6 @@ void testPubSubDomainOverride() {
423400
});
424401
}
425402

426-
private String getBrokerUrl(CachingConnectionFactory connectionFactory) {
427-
assertThat(connectionFactory.getTargetConnectionFactory()).isInstanceOf(ActiveMQConnectionFactory.class);
428-
try {
429-
return ((ActiveMQConnectionFactory) connectionFactory.getTargetConnectionFactory()).toURI().toString();
430-
}
431-
catch (IOException ex) {
432-
throw new RuntimeException(ex);
433-
}
434-
}
435-
436403
@Test
437404
void enableJmsAutomatically() {
438405
this.contextRunner.withUserConfiguration(NoEnableJmsConfiguration.class)
@@ -444,7 +411,7 @@ void enableJmsAutomatically() {
444411
@Test
445412
void runtimeHintsAreRegisteredForBindingOfAcknowledgeMode() {
446413
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
447-
context.register(ArtemisAutoConfiguration.class, JmsAutoConfiguration.class);
414+
context.register(TestConfiguration2.class, JmsAutoConfiguration.class);
448415
TestGenerationContext generationContext = new TestGenerationContext();
449416
new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
450417
assertThat(RuntimeHintsPredicates.reflection().onMethod(AcknowledgeMode.class, "of").invoke())
@@ -462,7 +429,7 @@ static class TestConfiguration2 {
462429

463430
@Bean
464431
ConnectionFactory customConnectionFactory() {
465-
return new ActiveMQConnectionFactory();
432+
return mock(ConnectionFactory.class);
466433
}
467434

468435
}
@@ -593,12 +560,12 @@ static class TestConfiguration10 {
593560

594561
@Bean
595562
ConnectionFactory connectionFactory1() {
596-
return new ActiveMQConnectionFactory();
563+
return mock(ConnectionFactory.class);
597564
}
598565

599566
@Bean
600567
ConnectionFactory connectionFactory2() {
601-
return new ActiveMQConnectionFactory();
568+
return mock(ConnectionFactory.class);
602569
}
603570

604571
}

0 commit comments

Comments
 (0)