我在 Spring Container 中有一个简单的测试:
public class JpaCategoryRepositoryTest extends AbstractJpaJavaTestBase {
@Inject
private CategoryService categoryService;
@Test
public void testStoreCategory(){
final Category category = new CategoryBuilder()
.name("Test category")
.build();
assertEquals("Cateogory ID is not assigned", 0L, category.getId());
categoryService.storeCategory(category);
assertNotEquals("Category ID is persistent", 0L, category.getId());
}
}
assertNotEquals
失败。我认为该事务尚未提交。好的,我已经更新了添加事务管理的测试:
public class JpaCategoryRepositoryTest extends AbstractJpaJavaTestBase {
@Inject
private CategoryService categoryService;
@Inject
TransactionTemplate transactionTemplate;
@Test
public void testStoreCategory(){
final Category category = new CategoryBuilder()
.name("Test category")
.build();
assertEquals("Cateogory ID is not assigned", 0L, category.getId());
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
categoryService.storeCategory(category);
return null;
}
});
assertNotEquals("Category ID is persistent", 0L, category.getId());
}
}
但这并没有帮助。在集成测试期间测试实体是否已保存的最佳模式是什么?实际实体被保存,当我在测试失败后检查表时。