9

JUnit@BeforeClassとSpring@TestExecutionListenerbeforeTestClass(TestContext testContext) "hook"の使用の違いは何ですか?違いがある場合、どの状況でどちらを使用しますか?

Mavenの依存関係:
spring-core:3.0.6.RELEASE
spring-context:3.0.6.RELEASE
spring-test:3.0.6.RELEASE
spring-data-commons-core:1.2.0.M1
spring-data-mongodb:1.0 .0.M4
mongo-java-driver:2.7.3
junit:4.9
cglib:2.2

JUnit @BeforeClassアノテーションの使用:

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = { "classpath:test-config.xml" })
public class TestNothing extends AbstractJUnit4SpringContextTests {

    @Autowired
    PersonRepository repo;

    @BeforeClass
    public static void runBefore() {
        System.out.println("@BeforeClass: set up.");
    }

    @Test
    public void testInit() {
        Assert.assertTrue(repo.findAll().size() == 0 );
    }
}

=> @BeforeClass: set up.
=> Process finished with exit code 0

スプリングフックの使用:

(1)beforeTestClass(TextContext testContext)をオーバーライドします:

import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;

public class BeforeClassHook extends AbstractTestExecutionListener {

    public BeforeClassHook() { }

    @Override
    public void beforeTestClass(TestContext testContext) {
        System.out.println("BeforeClassHook.beforeTestClass(): set up.");
    }
}

(2)@TestExecutionListenersアノテーションを使用します。

import org.springframework.test.context.TestExecutionListeners;  
// other imports are the same    

@ContextConfiguration(locations = { "classpath:test-config.xml" })
@TestExecutionListeners(BeforeClassHook.class)
public class TestNothing extends AbstractJUnit4SpringContextTests {

    @Autowired
    PersonRepository repo;

    @Test
    public void testInit() {
        Assert.assertTrue(repo.findAll().size() == 0 );
    }
}

=> BeforeClassHook.beforeTestClass(): set up.
=> Process finished with exit code 0
4

2 に答える 2

21

TestExecutionListenersテストを計測する再利用可能なコードを外部化する方法です。

そのため、実装するTestExecutionListener場合は、ニーズに応じて、テストクラス階層全体で、場合によってはプロジェクト間で再利用できます。

反対に、@BeforeClassメソッドは当然、単一のテストクラス階層内でのみ使用できます。

ただし、JUnitはルールもサポートしていることに注意してください。実装すると、同じことを実現するためorg.junit.rules.TestRuleにそれを宣言できます@ClassRule...JUnitルールをSpringのように再利用できるという追加の利点がありますTestExecutionListener

したがって、それは実際にはユースケースによって異なります。単一のテストクラスまたは単一のテストクラス階層で「クラス前」機能のみを使用する必要がある場合は、@BeforeClassメソッドを実装するだけの単純なルートを使用する方がよいでしょう。ただし、異なるテストクラス階層またはプロジェクト間で「クラス前」機能が必要になると予測される場合は、カスタムTestExecutionListenerルールまたはJUnitルールの実装を検討する必要があります。

JUnitルールに対するSpringの利点はTestExecutionListener、JUnitルールがTestExecutionListenerアクセスできないTestContextSpringApplicationContextにアクセスできることです。さらに、aTestExecutionListener自動的に検出され注文されます。

関連リソース

よろしく、

サム(Spring TestContext Frameworkの作成者)

于 2012-10-04T16:44:31.893 に答える
2

@BeforeClassを使用した最初のソリューションには、アプリケーションコンテキストがロードされていません。AbstractJUnit4SpringContextTestsを拡張し、@ContextConfigurationを定義しました。@beforeclassメソッドの前にコンテキストをロードする唯一の方法はlistnerだと思います。または、ここで説明したように、SpringJUnit4ClassRunnerクラスをさらに拡張する

于 2013-09-16T23:35:39.763 に答える