Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Testcontainers Support #112

Open
mbechto opened this issue Jun 26, 2024 · 4 comments
Open

Enhanced Testcontainers Support #112

mbechto opened this issue Jun 26, 2024 · 4 comments

Comments

@mbechto
Copy link

mbechto commented Jun 26, 2024

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.

  • ibmmq-jms-spring version: 3.3.1
  • Java version (including vendor and platform): Temurin-21.0.3+9
  • A small code sample that demonstrates the issue.
@mbechto
Copy link
Author

mbechto commented Jun 27, 2024

Hi @ibmmqmet, I forked the repository and had a go at implementing a very rudimentary PoC (see mbechto@30768a3), but maybe that gives an idea of the changes in order to achieve this. These are:

  • Extend the ConnectionDetails interface, allowing to provide alternative (to the properties) connection details
  • Using those as a preferred way of configuring the MQConnectionFactory, while falling back to properties
  • Implementing a factory that creates MQConnectionDetails sourced from the containers mapped random port
  • In order for that to work, implement an MQContainer

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

  • This requires Spring Boot 3, since 2 is missing the ConnectionDetails interface. It won't build with Spring Boot 2...
  • Architecturally, the Testcontainers related classes should reside in their own library or even upstream in spring-boot-starter-testcontainers
  • MQContainer is very rudimentary and needs more configuration options
  • Backward compatibility: I think I managed to keep the public static methods backward compatible, but I could have made a mistake along the way...

@eddumelendez
Copy link

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

@mbechto
Copy link
Author

mbechto commented Jun 28, 2024

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants