37

私は2つのテストを含むテストクラスを持っています:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContextTest.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)

public class MyITest extends implements BeanFactoryAware {

    private BeanFactory beanFactory;

    @Test
    public void test1() throws Exception {}

    @Test
    public void test2() throws Exception {}        
}

テストを個別に実行するとエラーは発生しませんが、すべてのテストを一緒に実行するとエラーが発生します。この失敗は、アプリケーション コンテキストを変更するいくつかのテストが原因です。

  b = beanFactory.getBean("logDataSource", BasicDataSource.class);
  b.set ...

このテストを個別に実行するオプションはありますか? test1 を開始するときに、必要なものをすべて読み取り、テストを実行してから、必要なものをすべて閉じたいだけです。そして、test2 を開始します。

4

1 に答える 1

69

You can use the @DirtiesContext annotation on the test class that modifies the application context.

Java Doc

Spring documentation

By default, this will mark the application context as dirty after the entire test class is run. If you would like to mark the context as dirty after a single test method, then you can either annotate the test method instead or set the classMode property to AFTER_EACH_TEST_METHOD at your class level annotation.

@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
于 2012-11-08T11:58:55.023 に答える