状況と問題点
いくつかのテスト クラスがあり、それぞれにいくつかのテスト メソッドがあります。すべてのテストは、バックグラウンドで同じテスト データベースを使用します。各テスト クラスはデータベースの内容を初期化し、いくつかのテスト メソッドで内容をテストします。
各テストを個別に実行すると、すべて合格します。しかし、(Maven または IDE、IntelliJ を使用して) 同時に複数のテストを実行すると、異なるテスト クラスのメソッドがインターリーブされて実行されます。2 番目のクラスのデータベースの初期化は、最初のクラスが開始された後、最初のクラスのすべてのテスト メソッドが実行される前に実行されるため、これらのメソッドは失敗します (データベースには既に 2 番目のクラスのデータが含まれているため)。
私が試したいくつかのこと、およびいくつかの詳細
最も簡単な解決策は、TestNG ランナーにクラスを連続して実行させることです (つまり、別のクラスのテスト メソッドを実行する前に、テスト クラスのすべてのテスト メソッドが終了するのを待ちます)。これはできますか?
スイートで各クラスを個別のテストとして指定することでおそらくこれを行うことができますが、不器用でエラーのあるテストクラスを追加するたびにスイートに何かを追加する必要があるため、これを行いたくありません-傾向があります。
TestNG に何も並列化しないように要求する (たとえば、スレッド カウントを 1 に設定する、または並列実行を無効にする) だけでは、メソッドが間違った順序で実行されるため (同時にではありません)、ここでは役に立ちません。
1 つのオプションは、テスト クラスごとに異なるデータベースを使用することですが、これを行う簡単な方法がわかりません (JPA と Guice を使用)。
現在、DBUnit、Unitils などは使用していません。これらのツールについてはよくわかりませんが、問題が解決しないという印象を受けました。
JPAを使用して、各テストクラスでデータベースを初期化しています(つまり、エンティティオブジェクトを作成して保存します)。