Spring测试:countRowsInTable结果不好

时间:2013-09-04 20:10:11

标签: java spring hibernate spring-test

我使用左import static org.springframework.test.jdbc.SimpleJdbcTestUtils.countRowsInTable;来测试Hibernate与HSQL数据库的连接 计算表中的行,但删除一行后结果仍然相同。我的代码出了什么问题?

更新:jdbcTemplate和Hibernate SessionFactory正在使用相同的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>

1 个答案:

答案 0 :(得分:0)

为确保获得预期的结果,您需要在每次断言之前刷新Hibernate会话。否则,某些内容可能会在会话中缓存而不会在数据库上执行。这样调用Hibrnate函数会返回正确的结果,因为它会知道缓存,但直接通过JDBC绕过它,将不会看到缓存并返回存储的东西。