我正在尝试使用AbstractTransactionalJUnit4SpringContextTests基类设置集成测试。我的目标非常简单:使用SimpleJDBCTemplate将一些数据插入数据库,使用DAO将其读回,然后回滚所有内容。jpa->hibernate是持久层。
对于我的测试,我创建了一个没有外键的数据库版本。这应该通过减少每个测试的fixture设置的数量来加速测试;在这个阶段,我对测试数据库完整性不感兴趣,只对我的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.
}
}
应用程序(一个SpringMVC站点)与DAO的工作正常。会发生什么?我从哪里开始寻找解决方案呢?
-
DAO的数据源与SimpleJDBCTemplate不同。
但是,不确定这会是怎样的,因为在daotests-context.xml文件中只定义了一个数据源。
-
Hibernate要求所有外键关系都存在,以便选择Gear对象。
有一些连接不存在,因为我正在对fld2/fld3/fld4中的连接进行硬编码。
-
DAO不会对未提交的数据执行操作。
但是为什么simplejdbctemplate会遵守这一点呢?我想他们都会做同样的事情。
-
内裤侏儒。
但是利润在哪里呢?