setUp() と setUpBeforeClass() の違い 質問する

setUp() と setUpBeforeClass() の違い 質問する

setUp()JUnit でユニットテストを行う場合、と という2 つの類似した方法があります。これらの方法の違いは何ですか? また、とsetUpBeforeClass()の違いは何ですか?tearDown()tearDownAfterClass()

署名は次のとおりです。

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

ベストアンサー1

@BeforeClassおよび注釈付きメソッド@AfterClassは、テスト実行中に、他のものが実行される前に、テスト全体の最初と最後に 1 回だけ実行されます。実際、これらはテスト クラスが構築される前に実行されるため、 で宣言する必要がありますstatic

メソッド@Before@Afterメソッドはすべてのテスト ケースの前後に実行されるため、テスト実行中に複数回実行される可能性があります。

クラスに 3 つのテストがあると仮定すると、メソッド呼び出しの順序は次のようになります。

setUpBeforeClass()

  (Test class first instance constructed and the following methods called on it)
    setUp()
    test1()
    tearDown()

  (Test class second instance constructed and the following methods called on it)
    setUp()
    test2()
    tearDown()

  (Test class third instance constructed and the following methods called on it)
    setUp()
    test3()
    tearDown()

tearDownAfterClass()

おすすめ記事