В чем разница между @BeforeClass и Spring @TestExecutionListener beforeTestClass ()

В чем разница между использованием JUnit @BeforeClass и Spring @TestExecutionListener beforeTestClass (TestContext testContext) & quot; hook & quot ;? Если есть разница, какой использовать при каких обстоятельствах?

Зависимости Maven:
весна-ядро: 3.0.6.RELEASE
весна-контекст: 3.0.6.RELEASE
весна-тест: 3.0.6.RELEASE
весна-данные-Обще-ядро: 1.2.0.M1
весна-данных MongoDB: 1.0.0.M4
Монго-ява-драйвер: 2.7.3
JUnit: 4,9
CGLIB: 2,2

Using JUnit @BeforeClass annotation:
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
Using the Spring hook:

(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

Ответы на вопрос(2)

Ваш ответ на вопрос