nietup
nietup

Reputation: 1

How to setup ActiveMQ broker for multiple Testcontainers

I am trying to setup testcontainers with ActiveMQ broker.

Here's my setup, this is the base test class, that other tests inherit from:

@SpringBootTest
@ActiveProfiles("test")
abstract class BaseIntegrationTest {
    companion object {
        @JvmStatic
        val activeMQContainer: GenericContainer<*> = GenericContainer("rmohr/activemq:5.15.9")
            .withExposedPorts(61616)
            .waitingFor(Wait.forListeningPort())

        @JvmStatic
        @BeforeAll
        fun setup() {
            activeMQContainer.start()
            System.setProperty(
                "jms.activemq.brokerUrl",
                "tcp://${activeMQContainer.host}:${activeMQContainer.getMappedPort(61616)}"
            )
        }

        @JvmStatic
        @AfterAll
        fun tearDown() {
            activeMQContainer.stop()
        }
    }
}

And here is the test jmsTemplate config:

@Configuration
@Profile("test")
class TestJmsConfig(
    @Value("\${jms.activemq.brokerUrl}") private val brokerUrl: String
) {
    @Bean
    fun connectionFactory(): ConnectionFactory {
        return ActiveMQConnectionFactory().apply {
            brokerURL = brokerUrl
        }
    }

    @Bean
    fun jmsTemplate(connectionFactory: ConnectionFactory): JmsTemplate {
        return JmsTemplate(connectionFactory)
    }
}

Here you can see the full setup: https://github.com/nietup/tcproblem

I get the following error on the failing test:

jakarta.servlet.ServletException: Request processing failed: org.springframework.jms.UncategorizedJmsException: Uncategorized exception occurred during JMS processing
...
Caused by: jakarta.jms.JMSException: Could not connect to broker URL: tcp://localhost:32769. Reason: java.net.ConnectException: Connection refused
...

Having one test class seems to be working, but the tests break when I have 2 test classes.

If I removed one of the test classes, the other one would pass.

I have set up simple tests like this:

@SpringBootTest
@AutoConfigureMockMvc
class NiceControllerIntegrationTest : BaseIntegrationTest() {

    @Autowired
    lateinit var mockMvc: MockMvc

    @Test
    fun `send nice message`() {
        mockMvc.perform(MockMvcRequestBuilders.post("/v1/nice/send"))
    }
}

where calling this endpoint triggers a message to be send to an ActiveMQ queue

And I expect them to pass. But when there is more than one test class, I get an error as described above

In my pom I am using

Upvotes: 0

Views: 86

Answers (0)

Related Questions