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

[Bug]: junit-jupiter brings junit 4 transitvely

Open snicoll opened this issue 2 years ago • 4 comments

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_

snicoll avatar Apr 24 '23 06:04 snicoll

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.

Jotschi avatar May 24 '23 21:05 Jotschi

I also found this issue, it is better to upgrade Junit 5 API and support Junit 4 using Junit 5 Vintage Engine

hantsy avatar May 30 '23 08:05 hantsy

This is a duplicate of https://github.com/testcontainers/testcontainers-java/issues/6921 and should be closed.

cc: @rnorth @kiview @eddumelendez @bsideup

Illutax avatar Jun 27 '23 09:06 Illutax

Root Cause

  1. 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'
    
  1. 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 @TestRule is required on classpath

cc: @eddumelendez

ericdriggs avatar Jan 16 '24 20:01 ericdriggs

the specific issue is that GenericContainer extends FailureDetectingExternalResource which implements TestRule from junit4

wakingrufus avatar Jul 18 '24 22:07 wakingrufus

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

ericdriggs avatar Jul 19 '24 20:07 ericdriggs

Junit 5 also supports Junit 4 Rules via VintageEngine. See the Junit documentation.

Illutax avatar Jul 20 '24 11:07 Illutax

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?

ericdriggs avatar Jul 22 '24 15:07 ericdriggs

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).

kiview avatar Jul 23 '24 14:07 kiview