ServiceConnectionContextCustomizer does not implement hashcode and equals so I think that any test class that currently uses @ServiceConnection will have its own context that can never be reused for other tests with the same configuration. This may not be what we want, although we'll have to consider the Testcontainers-managed lifecycle of @Container fields and how this will interact with caching and context reuse.

Comment From: philwebb

I think the key will need to be details from the @ServiceConnection annotation as well as the Container instance itself. I think we need to include the Container since there's so much that can be configured on it (e.g. password details might be configured differently).

This means that if two different tests redeclare the same container, they'll get a fresh ApplicationContext but if they extend a base class or implement an interface then they'll be able to use context caching.

Using the Container instance should also allow Testcontainers-managed lifecycles to work as long as tests are not run in parallel. A container stored in a cached context should get restarted before the tests run.

Comment From: andeb

I tried Spring Boot 3.1.0-RC1 for one project recently and noticed that the Spring context was getting initialised for every single scenario when using cucumber, instead of sharing state as documented by cucumber-spring.

The culprit seemed to be @ServiceAnnotation as it went back to normal when I removed it and went back to @DynamicPropertySource.

Do you guys think this is fixed by this issue? Unfortunately it is an enterprise project, so I cannot easily share what I have got at the moment but I can give it another try as soon as 3.1.0-RC2 is out.

Comment From: wilkinsona

@andeb Could you please try 3.1.0-SNAPSHOT that's available from https://repo.spring.io/snapshot and let us know?

Comment From: andeb

Thanks @wilkinsona. I can confirm this fixed the issue I had with Cucumber creating multiple contexts.

missCount stats for ApplicationContext cache remained at 1 and before it missed the cache for every single scenario execution.

Spring test ApplicationContext cache statistics: [DefaultContextCache@77da004a size = 1, maxSize = 32, parentContextCount = 0, hitCount = 1322, missCount = 1]

Comment From: wilkinsona

Thanks very much, @andeb.