testcontainers / testcontainers/testcontainers-java

Some errors cause a NoClassDefFoundError and the real error is hidden

Open
#1,872 4 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

resolution/acknowledged
Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in core/src/main/java/org/testcontainers/containers/GenericContainer.java around the referenced getDockerImageName() call and trace how construction triggers image resolution. Reproduce the example with gradle test and compare the default output with --info. Done means the underlying container or Docker error is diagnosable without being hidden behind NoClassDefFoundError.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java
Domain
devops, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.