2 回答

TA貢獻1853條經驗 獲得超9個贊
不,使用 Spring Data 存儲庫創建測試數據絕對沒有錯。
我什至更喜歡這樣,因為它通常允許更簡單的重構。
與在測試中使用 JPA 一樣,您需要記住 JPA 實現是后寫緩存。您可能希望EntityManager
在設置測試數據后刷新和清除數據,這樣您就不會從第一級緩存中獲得真正應該來自數據庫的任何內容。此外,這可確保數據實際寫入數據庫,并且會出現問題。
您可能對幾篇關于使用 Hibernate 進行測試的文章感興趣。他們不使用 Spring Data,但它可以與 Spring Data JPA 一起使用。

TA貢獻1784條經驗 獲得超8個贊
我建議Flyway用于設置您的數據庫并使用Flyway 測試擴展進行集成測試。
這樣你就可以做這樣的事情:
@ContextConfiguration(locations = {"/context/simple_applicationContext.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
FlywayTestExecutionListener.class})
@Test
@FlywayTest(locationsForMigrate = {"loadmsql"}) // execution once per class
public class MethodTest extends AbstractTestNGSpringContextTests {
@BeforeClass
@FlywayTest(locationsForMigrate = {"loadmsql"}) // execution once per class
public static void beforeClass() {
// maybe some additional things
}
@BeforeMethod
@FlywayTest(locationsForMigrate = {"loadmsql"}) // execution before each test method
public void beforeMethod() {
// maybe before every test method
}
@Test
@FlywayTest(locationsForMigrate = {"loadmsql"}) // as method annotation
public void simpleCountWithoutAny() {
// or just with an annotation above the test method where you need it
}
添加回答
舉報