2016-08-24 7 views
2

私は練習で春とキュウリを統合する必要があります。しかし、私は春の@Sql注釈でいくつかのテストデータをインポートすることはできません。しかし、キュウリを使用しない他の統合テストはうまく動作します。なぜ見つからなかったのですか?キュウリのランナー:春とキュウリの統合:@Sqlインポートが実行されなかった

@RunWith(Cucumber.class) 
@CucumberOptions(features={"classpath:features/"}) 
public class CucumberIT { 

} 

そして、ステップdefination:

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes={DevDbConfig.class}) 
@Sql("classpath:test-reader-data.sql") 
@Transactional 
@ActiveProfiles("dev") 
public class StepDefines { 

    @Autowired 
    ReaderService readerService; 

    Reader reader; 

    @Given("^a user with name Lily$") 
    public void a_user_with_name_Lily() throws Throwable { 
    reader = readerService.findByName("Lily"); // reader is null here! 
    } 

    @Given("^this user Lily exists$") 
    public void this_user_Lily_exists() throws Throwable { 
     assertThat(reader, notNullValue()); 
    } 

    @Then("^Lily's info should be returned$") 
    public void lily_s_info_should_be_returned() throws Throwable { 
     assertThat(reader.getName(), is("Lily")); 
    } 

} 

しかし、次のテストコードは、通常、試験データをインポートすることができます。

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes={DevDbConfig.class}) 
@Sql("classpath:test-reader-data.sql") 
@Transactional 
@ActiveProfiles("dev") 
public class ReaderServiceTest { 

    @Autowired 
    ReaderService readerService; 

    @Autowired 
    EntityManager entityManager; 

    @Test 
    public void testQueryByIdAndShouldNotReturnNull() {  
    Reader reader = readerService.findByName("Lily"); 
    assertThat(reader, notNullValue()); // reader is not null! 
    } 

} 

感謝を!

答えて

2

@SqlSqlScriptsTestExecutionListenerで実行されます。 SpringJUnit4ClassRunnerは、TestExecutionListenerのすべてを登録します。

SqlScriptsTestExecutionListener Javaのドキュメントから、それは言う:

スクリプトとインライン文がexecutionPhaseフラグの設定された値に応じて、対応するテストメソッドの実行前または後に実行されます。

だから、クラスレベル@Sqlは、個々の@Sql注釈を持つすべての@Test方法と同じです。

しかし、キュウリが実行中の場合、@Testメソッドは実行されず、@Sqlは実行される機会がありません。

最後に、私はそれを自分自身を実行する必要があります。

@Autowired 
    DataSource ds; 

    @Given("^a user with name Lily$") 
    public void a_user_with_name_Lily() throws Throwable { 
    ScriptUtils.executeSqlScript(ds.getConnection(), new ClassPathResource("test-reader-data.sql")); 
    reader = readerService.findByName("Lily"); 
    } 

    @Given("^this user Lily exists$") 
    public void this_user_Lily_exists() throws Throwable { 
    assertThat(reader, notNullValue()); 
    } 

    @Then("^Lily's info should be returned$") 
    public void lily_s_info_should_be_returned() throws Throwable { 
    assertThat(reader.getName(), is("Lily")); 
    } 
関連する問題