Skip to content

Support to expose host ports via @Testcontainers for JUnit Jupiter - #4577

Closed
xhaggi wants to merge 1 commit into
testcontainers:masterfrom
xhaggi:junit-jupiter-expose-host-ports
Closed

Support to expose host ports via @Testcontainers for JUnit Jupiter#4577
xhaggi wants to merge 1 commit into
testcontainers:masterfrom
xhaggi:junit-jupiter-expose-host-ports

Conversation

@xhaggi

@xhaggi xhaggi commented Oct 13, 2021

Copy link
Copy Markdown

Currently, it is not possible to expose host ports when using the @Testcontainers annotation in JUnit Jupiter due to the execution order of @BeforeAll [1] and the registration order of extensions added with @ExtendWith [2]. A method annotated with @BeforeAll within a test class annotated with @Testcontainers is executed after the @BeforeAll of the Testcontainers extension. If you use another extension such as WireMock in combination with @Testcontainers, the same problem occurs. The documentation [3] notes that exposing host ports should be invoked before containers are started, but after the server is started on the host.

This change adds exposeHostPorts to @Testcontainers and configures it before the containers are started.

[1] https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/BeforeAll.html
[2] https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtendWith.html
[3] https://www.testcontainers.org/features/networking/#exposing-host-ports-to-the-container

@bsideup

bsideup commented Oct 15, 2021

Copy link
Copy Markdown
Member

FYI #4584 provides an alternative to this that is framework agnostic, could you please have a look?

@xhaggi

xhaggi commented Oct 20, 2021

Copy link
Copy Markdown
Author

Could you please enlighten me how this solves the problem when using @Testcontainers in Junit Jupiter?

@kiview

kiview commented Oct 20, 2021

Copy link
Copy Markdown
Member

Use withAccessToHost(true) on the containers that are instrumented by @Testcontainers and call exposeHostPorts() e.g. in your @BeforeAll method (can happen in this case after container startup).

Please share further information, why this would not solve your use case.

@xhaggi

xhaggi commented Oct 20, 2021

Copy link
Copy Markdown
Author

@kiview thanks, I'll give it a try and let you know if it works.

@xhaggi

xhaggi commented Oct 20, 2021

Copy link
Copy Markdown
Author

It works, thank you. I closed the PR.

@xhaggi xhaggi closed this Oct 20, 2021
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

Successfully merging this pull request may close these issues.

4 participants