AbstractTransactionalJUnit4SpringContextTests:无法获取 dao 来查找插入的数据
我正在尝试使用 AbstractTransactionalJUnit4SpringContextTests 基类设置集成测试。我的目标非常简单:使用 simpleJdbcTemplate 将一些数据插入数据库,使用 DAO 将其读回,然后回滚所有内容。 JPA->Hibernate是持久层。
对于我的测试,我创建了一个没有外键的数据库版本。这应该可以减少每次测试的夹具设置数量,从而加快测试速度;在这个阶段,我对测试数据库完整性不感兴趣,只对 HQL 中的业务逻辑感兴趣。
/* DAO */
@Transactional
@Repository("gearDao")
public class GearDaoImpl implements GearDao {
@PersistenceContext
private EntityManager entityManager;
/* Properties go here */
public Gear findById(Long id) {
return entityManager.find(Gear.class, id);
}
}
/* Test Page */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/com/dom/app/dao/DaoTests-context.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
public class GearDaoImplTests extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private GearDao gearDao;
@Test
@Rollback(true)
public void quickTest() {
String sql;
// fields renamed to protect the innocent :-)
sql = "INSERT INTO Gear (Gear_Id, fld2, fld3, fld4, fld5, fld6, fld7) " +
" VALUES (?,?,?,?,?,?,?)";
simpleJdbcTemplate.update(sql, 1L, 1L, 1L, "fld4", "fld5", new Date(), "fld7");
assertEquals(1L, simpleJdbcTemplate.queryForLong("select Gear_Id from Gear where Gear_Id = 1"));
System.out.println(gearDao);
Gear gear = gearDao.findById(1L);
assertNotNull("gear is null.", gear); // <== This fails.
}
}
该应用程序(Spring MVC 站点)与 DAO 配合良好。可能发生什么情况?我应该从哪里开始寻找解决方案?
- DAO 不知何故具有与 simpleJdbcTemplate 不同的数据源。但不确定这会如何,因为 DaoTests-context.xml 文件中只定义了一个数据源。
- Hibernate 要求存在所有外键关系才能选择 Gear 对象。有一些连接不存在,因为我将这些连接硬编码在 fld2/fld3/fld4 中。
- DAO 不会对未提交的数据采取行动。但是为什么 simpleJdbcTemplate 会遵守这一点呢?我假设他们都做同样的事情。
- 内裤侏儒。但是利润在哪里呢?
I'm trying to set up integration tests using the AbstractTransactionalJUnit4SpringContextTests base class. My goal is really simple: insert some data into the database using the simpleJdbcTemplate, read it back out using a DAO, and roll everything back. JPA->Hibernate is the persistence layer.
For my tests, I've created a version of the database that has no foreign keys. This should speed up testing by reducing the amount of fixture setup for each test; at this stage I'm not interested in testing the DB integrity, just the business logic in my HQL.
/* DAO */
@Transactional
@Repository("gearDao")
public class GearDaoImpl implements GearDao {
@PersistenceContext
private EntityManager entityManager;
/* Properties go here */
public Gear findById(Long id) {
return entityManager.find(Gear.class, id);
}
}
/* Test Page */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/com/dom/app/dao/DaoTests-context.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
public class GearDaoImplTests extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private GearDao gearDao;
@Test
@Rollback(true)
public void quickTest() {
String sql;
// fields renamed to protect the innocent :-)
sql = "INSERT INTO Gear (Gear_Id, fld2, fld3, fld4, fld5, fld6, fld7) " +
" VALUES (?,?,?,?,?,?,?)";
simpleJdbcTemplate.update(sql, 1L, 1L, 1L, "fld4", "fld5", new Date(), "fld7");
assertEquals(1L, simpleJdbcTemplate.queryForLong("select Gear_Id from Gear where Gear_Id = 1"));
System.out.println(gearDao);
Gear gear = gearDao.findById(1L);
assertNotNull("gear is null.", gear); // <== This fails.
}
}
The application (a Spring MVC site) works fine with the DAO's. What could be happening? And where would I begin to look for a solution?
- The DAO somehow has a different dataSource than the simpleJdbcTemplate. Not sure how this would be, though, since there's only one dataSource defined in the DaoTests-context.xml file.
- Hibernate requires all foreign key relations to be present in order to select out the Gear object. There are a couple of joins that are not present since I'm hardcoding those in fld2/fld3/fld4.
- The DAO won't act on the uncommitted data. But why would the simpleJdbcTemplate honor this? I'd assume they both do the same thing.
- Underpants gnomes. But where's the profit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几个小时的睡眠会有多大的改变。我醒来后想:“我应该检查日志以查看实际执行的查询。”当然,事实证明休眠被配置为为一些外键生成一些内部连接。一旦我提供了这些依赖项,它就像一个魅力。
我喜欢每个测试概念的自动回滚。集成测试,我来了!
What a difference a couple hours of sleep makes. I woke up and thought "I should check the logs to see what query is actually being executed." And of course it turns out that hibernate was configured to generate some inner joins for a few of the foreign keys. Once I supplied those dependencies it worked like a charm.
I'm loving the automatic rollback on every test concept. Integration tests, here I come!