testcontainers / testcontainers/testcontainers-java

Some errors cause a NoClassDefFoundError and the real error is hidden

Aperta
#1,872 4 commenti 11 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

resolution/acknowledged
Lingua principale
Java
Stelle
8.7k
Fork
1.9k
Merge medio
2g 17h
PR unite (30g)
9

Descrizione

In certain cases a test may fail with a NoClassDefFoundError, and it's a nightmare to find the real error.

For example:

static final PostgreSQLContainer = PostgreSQLContainer("postgresxxx:11.5") 
    .withDatabaseName("netonomy")
    .withUsername("postgres")
    .withPassword("password");

Then running gradle test results in:

com.example.SomeIntegrationTest > testPostgres() FAILED
    java.lang.NoClassDefFoundError: Could not initialize class com.example.SomeIntegrationTest

To find out the actual error, it was necessary to run Gradle with --info. I only used a bad image name here as an example to reproduce - the real error was "Docker environment should have more than 2GB free disk space", and it happened only on the CI build agent - so this was a real nightmare to diagnose!

What makes this even more difficult to pin down is the fact that even removing the @Testcontainers and @Container still results in the exact same behaviour!

The reason for all of this, is the fact that GenericContainer's constructor calls this.setDockerImageName() which in turn calls getDockerImageName() which actually attempts to get the image: https://github.com/testcontainers/testcontainers-java/blob/master/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L1057

IMO this is an action that should not take place in a constructor - this should happen only later during start(). Either way there ought to be some better solution, because diagnosing errors this way is a nightmare.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in core/src/main/java/org/testcontainers/containers/GenericContainer.java, intorno alla chiamata referenziata a getDockerImageName(), e segui come la costruzione attiva la risoluzione dell’immagine. Riproduci l’esempio con gradle test e confronta l’output predefinito con --info. È completato quando l’errore sottostante del container o di Docker è diagnosticabile senza essere nascosto dietro NoClassDefFoundError.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
docker, java
Ambito
devops, testing
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.