testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

Make TestcontainersExtension public

Open hmatt1 opened this issue 3 years ago • 4 comments

Proposed fix for https://github.com/testcontainers/testcontainers-java/issues/2045

When we use multiple JUnit5 extensions, the way to order them is via the @ExtendsWith({FirstExtension.class, SecondExtension.class}) annotation. But TestcontainersExtension is not public so we can only use the @Testcontainers annotion and we are not able to order the extension.

See the following documentation on JUnit5: https://junit.org/junit5/docs/current/user-guide/#extensions-registration-declarative

hmatt1 avatar Apr 26 '22 19:04 hmatt1

Hi @hmatt1, sorry for not reacting sooner. Can you give me an example of a list of extensions, where the order with regards to the TestcontainersExtension matters?

Looking around different 3rd party Jupiter extensions, (like junit-pioneer), I get the feeling that having the Extension class package-private is the idiomatic way to do things.

kiview avatar Jul 06 '22 13:07 kiview

@kiview It is mentioned in the issue that one user wanted to do some registry authentication config: https://github.com/testcontainers/testcontainers-java/issues/2045#issuecomment-553611905

The issue also mentions

Some microservices frameworks have extension to start the application for integration testing, whe using test containers, we need to be sure that the testcontainers extension run first to start the containers before the apps starts.

I'm not familiar with the specific extensions the users are referring to. However, in the docs I linked to, they provide this example of defining a custom composed annotation that combines multiple extensions in a reusable way.

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith({ DatabaseExtension.class, WebServerExtension.class })
public @interface DatabaseAndWebServerExtension {
}

This is another use case that isn't supported when the TestcontainersExtension class is package-private. This is something I wanted to take advantage of in my project, since we have a few extensions we are using across our tests and would like to define them in one place.

hmatt1 avatar Jul 06 '22 14:07 hmatt1

Thanks for providing more info @hmatt1. I was talking with @sormuras from the JUnit team and he generally recommended to not go for public as the default visibility modifier and that extensions should strive to be independent.

I can imagine that there might be concrete cases where this ordering is the only way to make it work, but I wanted to know the concrete case, to understand if there might be a better approach (similar to what e.g. Spring provides with DynamicPropertySource).

kiview avatar Jul 06 '22 19:07 kiview

@kiview Is the use case around creating a composed annotation that includes the TestContainers extension still valid? My understanding is that it is not possible if the extension isn't public.

hmatt1 avatar Jul 07 '22 16:07 hmatt1

@hmatt1 Thanks for providing the PR, after some consideration and thanks to the feedback of the community, we decided to make the extension public 🙂

kiview avatar Aug 16 '22 15:08 kiview