testcontainers-java
testcontainers-java copied to clipboard
[Bug]: junit-jupiter brings junit 4 transitvely
Module
Core
Testcontainers version
1.18.0
Using the latest Testcontainers version?
Yes
Host OS
MacOs
Host Arch
x86
Docker version
irrelevant
What happened?
Adding a dependency on org.testcontainers:junit-jupiter:jar:1.18.0 (at least with Maven) brings junit:junit:jar:4.13.2 on the test classpath.
Relevant log output
\- org.testcontainers:junit-jupiter:jar:1.18.0:test
[INFO] \- org.testcontainers:testcontainers:jar:1.18.0:test
[INFO] +- junit:junit:jar:4.13.2:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:2.2:test
[INFO] +- org.apache.commons:commons-compress:jar:1.22:test
[INFO] +- org.rnorth.duct-tape:duct-tape:jar:1.0.8:test
[INFO] | \- org.jetbrains:annotations:jar:17.0.0:test
[INFO] +- com.github.docker-java:docker-java-api:jar:3.3.0:test
[INFO] | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.15.0-rc3:test
[INFO] \- com.github.docker-java:docker-java-transport-zerodep:jar:3.3.0:test
[INFO] +- com.github.docker-java:docker-java-transport:jar:3.3.0:test
[INFO] \- net.java.dev.jna:jna:jar:5.12.1:test
### Additional Information
_No response_
The problem is also that you can't even manually exclude it. Somehow the testcontainer jupiter API indirectly references it and custom container classes can't be compiled without junit 4 being present on the classpath. The only solution I found is to add junit 4 back to the classpath.
I also found this issue, it is better to upgrade Junit 5 API and support Junit 4 using Junit 5 Vintage Engine
This is a duplicate of https://github.com/testcontainers/testcontainers-java/issues/6921 and should be closed.
cc: @rnorth @kiview @eddumelendez @bsideup
Root Cause
- core module has an api dependency on junit 4.
-
https://github.com/testcontainers/testcontainers-java/blob/main/core/build.gradle#L72
dependencies { api 'junit:junit:4.13.2'
- Generic module requires Junit 4 TestRule
- https://github.com/testcontainers/testcontainers-java/issues/970
Blocked on
- Test containers version 2 since breaking change junit4 -> junit5 ** see: https://github.com/testcontainers/testcontainers-java/issues/970
Workaround
- no workaround exists since junit 4
@TestRuleis required on classpath
cc: @eddumelendez
the specific issue is that GenericContainer extends FailureDetectingExternalResource which implements TestRule from junit4
Yes, a fix would require a complete refactor from Junit4 TestRule to Junit5 AfterEachCallback and BeforeEachCallback.
Since there's no common interface, this would be a major version breaking change with the consequence of requiring Junit5 and not supporting Junit4 for version 2.*.*
https://www.baeldung.com/junit-5-migration
Junit 5 also supports Junit 4 Rules via VintageEngine. See the Junit documentation.
It seems like vintage engine might provide value to testcontainers project as a transitionary point. From a client perspective it seems roughly equivalent, since the junit4 annotations are still used and you're effectively just shifting the name of the dependency artifact from junit:junit:4.* to org.junit.vintage:junit-vintage-engine:5.*. Am I missing something?
Duplicates https://github.com/testcontainers/testcontainers-java/issues/970.
There are a couple of stub dependencies for JUnit4 out there, that can be used as a workaround (e.g. https://github.com/xdev-software/testcontainers-junit4-mock).