arquillian / arquillian-testcontainers

Arquillian loadable extension for test-container support
Apache License 2.0
5 stars 4 forks source link

Reuse @TestContainers and @Container #11

Open hantsy opened 4 months ago

hantsy commented 4 months ago

I found there is a new annotation @TestContainer added to control testcontainer Container. https://github.com/arquillian/arquillian-testcontainers/blob/main/src/main/java/org/jboss/arquillian/testcontainers/TestContainersObserver.java#L28

The basic support of Spring Boot testcontiners just adds a @ServiceConnection to the container declarations to setup the connection properties automatically.

@SpringBootTest
@Testcontainers// from testcontainers
class MyTeset {

   @Container // from testcontainers
   @ServiceConnection // from spring Boot
   static AContainer container = ...
}

If possible, reuse the testcontainers existing @Testcontainers and @Container to controll the container lifecycle, and ensure Arquillian sets up the application server adapter's properties after the successful waiting strategy. Then perform the deployment that is defined by @Deployment.

jamezp commented 4 months ago

Those annotations will not work in all cases. For example, in WildFly there is the ability to implement a ServerSetupTask which allows for configuration of WildFly before the deployment is done. You cannot inject the same instance of the Testcontainer in the ServerSetupTask without Arquillian managing the lifecyle of the Testcontainer.

hantsy commented 4 months ago

Testcontainers have wait strategies, can the Wildfly/Glassfishs test deployment step wait for testconainters be ready for deployment?

jamezp commented 4 months ago

@hantsy Yes, that should be possible AFAIK. If you have an example of what it would look like without Arquillian, I can confirm.

What should happen though is the Testcontainer is started before the class. The class should then be created and the deployment done. Once the class is complete, the Testcontainer should stop.

hantsy commented 4 months ago

I tried this way in an example before: https://github.com/hantsy/arquillian-testcontainers-example/blob/autodeployment/src/test/java/com/example/it/GreetingResourceTest.java#L51 (ignore this example in autodeployment branch, I was trying to use AutoDeployment provided in Arquillian, but not sure why the deployment was not started)

The @Container and @Deployment archives can be ready before running tests, Arquillian can utilize the wait strategy provided by Testcontainers, etc. to check the startup status of the application server and setup Arqullian properties for application servers, and decide to deploy.

jamezp commented 4 months ago

That could be done by extending the testcontainer. Note that the @Testcontainer annotation for JUnit 5 in the Testcontainer project is different than this. This project is for injecting a GenericContainer<?> as an @ArquillianResource.