2013-03-29 8 views
13

カスタムTestExecutionListenerSpringJUnit4ClassRunnerを組み合わせて、テストデータベースでLiquibaseスキーマのセットアップを実行します。私のTestExecutionListenerはうまくいきますが、私のクラスで注釈を使用すると、テスト中のDAOの注入が機能しなくなりました。少なくともインスタンスはnullです。TestExecutionListenerを使用しているときにSpringテスト注入が機能しない

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext-test.xml" }) 
@TestExecutionListeners({ LiquibaseTestExecutionListener.class }) 
@LiquibaseChangeSet(changeSetLocations={"liquibase/v001/createTables.xml"}) 
public class DeviceDAOTest { 

    ... 

    @Inject 
    DeviceDAO deviceDAO; 

    @Test 
    public void findByCategory_categoryHasSubCategories_returnsAllDescendantsDevices() { 
     List<Device> devices = deviceDAO.findByCategory(1); // deviceDAO null -> NPE 
     ... 
    } 
} 

リスナーが非常に簡単です:

public class LiquibaseTestExecutionListener extends AbstractTestExecutionListener { 

    @Override 
    public void beforeTestClass(TestContext testContext) throws Exception { 
     final LiquibaseChangeSet annotation = AnnotationUtils.findAnnotation(testContext.getTestClass(), 
       LiquibaseChangeSet.class); 
     if (annotation != null) { 
      executeChangesets(testContext, annotation.changeSetLocations()); 
     } 
    } 

    private void executeChangesets(TestContext testContext, String[] changeSetLocation) throws SQLException, 
      LiquibaseException { 
     for (String location : changeSetLocation) { 
      DataSource datasource = testContext.getApplicationContext().getBean(DataSource.class); 
      DatabaseConnection database = new JdbcConnection(datasource.getConnection()); 
      Liquibase liquibase = new Liquibase(location, new FileSystemResourceAccessor(), database); 
      liquibase.update(null); 
     } 
    } 

} 

ログ内のエラーは私のテストでちょうどNullPointerException、ありません。私は私のTestExecutionListenerの使用がautowiringまたは注入にどのように影響するかはわかりません。

答えて

20

私は春のDEBUGログを見て、私自身のTestExecutionListenerスプリングを省略すると、DependencyInjectionTestExecutionListenerを適切に設定することがわかりました。リスナーが上書きされる@TestExecutionListenersでテストに注釈を付けるとき。

だから私はちょうど私のカスタム1で明示的にDependencyInjectionTestExecutionListenerを追加し、すべてが正常に動作します:

​​

はUPDATE: 行動がhere文書化されています。

... @TestExecutionListenersで明示的にクラスを構成し、リスナーのリストからDependencyInjectionTestExecutionListener.classを省略することで、依存性注入を完全に無効にすることができます。

+1

それは正しいです: '@ TestExecutionListeners'を介してカスタム' TestExecutionListener'を指定すると、デフォルトのTestExecutionListenersのすべてが暗黙的にオーバーライドされます。確かに、この機能はあまりよく書かれていません。ですから、JIRAの問題を開いてドキュメントの改善を要請してください。 ;) –

+1

@SamBrannen:実際にはそれは文書化されています。私の更新された答えを見てください。 – nansen

+2

私はそれを書いて以来、あなたが引用したテキストをよく知っています。 ;)しかし、それはあなたが遭遇したシナリオを明示的に記述していません。そのため、ドキュメントを改善するためにJIRAチケットを開くことをお勧めしました。 –

3

私はちょうど同じような何かを行うことを検討することをお勧めします:

@TestExecutionListeners(
     mergeMode =TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS, 
     listeners = {MySuperfancyListener.class} 
) 

をので、あなたが必要としているリスナー知る必要はありません。 SpringBootがちょうどDependencyInjectionTestExecutionListener.classを使って正しく動作させようとしているので、このアプローチをお勧めします。

関連する問題