这是一个非常简单的测试,关于在数据库中持久化一个瞬态 Person 对象,然后验证保存在数据库中的对象是否与瞬态 Person 对象相同。这里是测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/daoIntegration-test.xml")
@Transactional
public class HibernatePersonDaoIntegrationTest {
@Autowired
private PersonDao PersonDao;
@Autowired
private SessionFactory sessionFactory;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void shouldSavePerson() {
//Given
Person person = new Person();
//When
PersonDao.savePerson(person);
//Then ----THIS ASSERTION PASSES!!!
assertThat(person.getId(), notNullValue());
//And ----THIS ONE FAILS!!!
Person persistedPerson = jdbcTemplate.queryForObject("select * from table_Person", Person.class);
assertThat(persistedPerson, is(person));
}
我的 daoIntegration-test.xml
<jdbc:embedded-database id="dataSource" type="HSQL"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="com.domain"/>
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
域类是:
@Entity
@Table(name="table_Person")
public class Person {
@Id
@TableGenerator(name = "seq_table", table = "GENERATOR_TABLE")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "seq_table")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private Long id;
private String personFirstName;
...
第一个断言通过,但第二个断言失败,我在某个地方犯了一个愚蠢的错误,但不知道在哪里!!!