我对 HSQL 数据库的 Hibernate 连接进行了以下测试,使用
import static org.springframework.test.jdbc.SimpleJdbcTestUtils.countRowsInTable;
来计算表中的行数,但删除一行后结果仍然相同。我的代码有什么问题?
更新:jdbcTemplate
和 HibernateSessionFactory
使用相同的 dataSource bean。
package com.habuma.spitter.persistence;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.jdbc.SimpleJdbcTestUtils.countRowsInTable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
import com.habuma.spitter.domain.Spitter;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:persistence-context.xml",
"classpath:test-dataSource-context.xml",
"classpath:test-transaction-context.xml"})
@TransactionConfiguration(transactionManager="txMgr", defaultRollback=true)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
@Transactional
public class DataAccessUnitTestTemplate {
@Autowired
private ApplicationContext context;
@Autowired
private SpitterDao spitterDao;
@Autowired
private SimpleJdbcTemplate jdbcTemplate;
@Test
public void testDataAccess() {
Spitter spitter = new Spitter();
spitter.setFullName("First");
spitter.setEmail("one@gmail.com");
spitter.setUsername("onefirst");
spitter.setPassword("password");
spitter.setUpdateByEmail(false);
//add to database
spitterDao.addSpitter(spitter);
//update user with id = 3
Spitter toChangeSpitter = spitterDao.getSpitterById(3);
toChangeSpitter.setFullName("NULL");
spitterDao.saveSpitter(toChangeSpitter);
//add next user
spitter = new Spitter();
spitter.setFullName("Second");
spitter.setEmail("two@gmail.com");
spitter.setUsername("twosecond");
spitter.setPassword("pass");
spitter.setUpdateByEmail(false);
spitterDao.addSpitter(spitter);
assertEquals(4, countRowsInTable(jdbcTemplate, "spitter"));
for(int i = 1; i <= 4; i++) {
spitter = spitterDao.getSpitterById((long)i);
System.out.println(spitter.getId() + " : " + spitter.getUsername() + " : " + spitter.getFullName());
}
//delete user with ID = 4
spitterDao.deleteSpitter(4);
System.out.println("\n");
for(int i = 1; i <= 3; i++) {
spitter = spitterDao.getSpitterById((long)i);
System.out.println(spitter.getId() + " : " + spitter.getUsername() + " : " + spitter.getFullName());
}
System.out.println("Ehm ... " + spitterDao.getSpitterById(4)); //null
assertEquals(3, countRowsInTable(jdbcTemplate, "spitter")); //assertion ? rows still 4
}
}
这是输出
1 : habuma : Craig Walls
2 : artnames : Art Names
3 : onefirst : NULL
4 : twosecond : Second
1 : habuma : Craig Walls
2 : artnames : Art Names
3 : onefirst : NULL
Ehm ... null
>>!Assertion!<<
java.lang.AssertionError: expected:<3> but was:<4>