testcontainers / testcontainers/testcontainers-java

Spring Boot Tests with JUnit5 Jupiter and Shared Containers

オープン
#2,290 コメント 47 件 リアクション 12 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

resolution/waiting-for-info
主要言語
Java
スター
8.7k
フォーク
1.9k
平均マージ
2日 17時間
マージ済み PR(30日)
9

説明

Currently, a typical test setup with Spring Boot tests and shared containers does not work anymore with JUnit 5: Using static testcontainers with Spring Boot's ApplicationContextInitializer to configure the server's ports in the Spring context.

The problem is that the Spring application context gets initialized during Jupiter extension's post processing while testcontainers are started during before all callbacks. The latter happens after post processing.

See
https://github.com/junit-team/junit5/blob/c9ae6e261550481362f17d21e88137a8c71fc7c8/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedTestDescriptor.java#L184
vs.
https://github.com/junit-team/junit5/blob/c9ae6e261550481362f17d21e88137a8c71fc7c8/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedTestDescriptor.java#L189

One approach to solve this could be to start the testcontainers also during post processing (TestInstancePostProcesso#postProcessTestInstancer)

Then the only remaining problem would be to ensure that the TestcontainersExtension always gets created before the SpringExtension.

Problematic Example

@SpringBootTest
@ContextConfiguration(initializers = {Initializer.class})
@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class JupiterSharedTestcontainersTests {

  @Container
  public static JdbcDatabaseContainer postgreSQLContainer = new PostgreSQLContainer();

  @Test
  public void testTomatoes() {
    assertTrue("tomatoes".equals("tomatoes"));
  }
  
  public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
      TestPropertyValues.of(
          "spring.datasource.url=jdbc:postgresql://"
              + postgreSQLContainer.getContainerIpAddress()
              + ":" + postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT)
              + "/db_name"
      ).applyTo(configurableApplicationContext.getEnvironment());
    }
  }

}

What do you think?

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue にある問題のある Spring Boot/Jupiter の例から始め、参照されている 184 行目と 189 行目付近の JUnit Jupiter ClassBasedTestDescriptor.java の順序を比較します。ApplicationContextInitializer、Testcontainers、Jupiter コールバック間の相互作用を再現し、そのうえで、他の拡張機能を壊すことなく共有コンテナーのセットアップを機能させられる、サポートされた順序付けが可能かどうかを判断します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
docker, java, postgresql, spring-boot
領域
testing-qa
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。