ibm-messaging / mq-jms-spring

Components to assist MQ JMS integration with Spring frameworks
Apache License 2.0
189 stars 102 forks source link

Enhanced Testcontainers Support #112

Open mbechto opened 3 months ago

mbechto commented 3 months ago

We were trying to use the mq-container with Spring Boot 3.x Testcontainers with @ServiceConnection similar to what you can do with a PostgreSQL container:

@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {
  @Bean
  @ServiceConnection
  @RestartScope
  PostgreSQLContainer<?> postgresContainer() {
    return new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"));
  }
}

Our intention is to avoid unnecessary container restarts between @SpringBootTests as that slows down the testing process significantly therefore @RestartScope. Maybe more importantly @ServiceConnection configures the correct connection properties, since Testcontainers exposes services on a random port (by default) this is crucial. So similar to PostgreSQL, we tried something like this:

@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {
  @Bean
  @ServiceConnection
  @RestartScope
  GenericContainer<?> ibmMqTestContainer() {
    return new GenericContainer<>(DockerImageName.parse("ibm-messaging/mq-container:latest"));
  }
}

Resulting in the following error on startup:

org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsNotFoundException: No ConnectionDetails found for source '@ServiceConnection source for Bean 'ibmMqTestContainer' defined in class path resource [TestcontainersConfiguration.class]'. You may need to add a 'name' to your @ServiceConnection annotation

After some debugging through Spring code, I guess this would require some integration to make it work, e.g. implementing some classes similar to the ActiveMQ Testcontainers integration.

mbechto commented 3 months ago

Hi @ibmmqmet, I forked the repository and had a go at implementing a very rudimentary PoC (see https://github.com/mbechto/mq-jms-spring/commit/30768a386c7ca9436d49da3b3260d000e44788ee), but maybe that gives an idea of the changes in order to achieve this. These are:

I have added an integration test to further illustrate the use case: https://github.com/mbechto/mq-jms-spring/blob/30768a386c7ca9436d49da3b3260d000e44788ee/mq-jms-spring-boot-starter/src/test/java/com/ibm/mq/spring/boot/TestcontainersTest.java

Caveats

eddumelendez commented 3 months ago

Hi, spring boot 3.1 should be required in order to support ConnectionDetails and ServiceConnection

mbechto commented 3 months ago

Hi @eddumelendez, thanks for the heads up! That's right. As I pointed out in my caveats section, the PoC I wrote does not support Spring 2 due to what you mentioned. So that poses a problem, which is one reason why I didn't create a PR from my fork yet.

ibmmqmet commented 2 months ago

Thanks for raising this. It looks like it might be interesting. I plan to dig deeper into it when I get some time. Without looking at all yet, one thing I want to be wary of is any bloat in the dependency chains.