5

我有与 hsqldb 的集成测试(在内存中),现在我需要在我的测试中设置保存点,在 BaseTest 类中,如何在测试中设置保存点(hsqldb(内存中))?

基础测试:

@ContextConfiguration(classes = {TestConfig.class})
public class BaseTest {

    @Before
    public void savePoint() {
        //set savepoint - How can do it this?

    }

    @After
    public void rollBackToSavePoint() {
        //roll back to savepoint - How can do it this?

    }
}

我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
public class MyTest extends BaseTest {

    @Test
    public void test1() {
     ...
    }
}

测试配置:

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@Configuration
@EnableJpaRepositories("database.dao")
@ComponentScan(basePackageClasses = { MyServiceImpl.class})
@EntityScan({"database.model"})
@Import({DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TestConfig {

...

}
4

1 回答 1

2

它帮助了我:

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Test
    public void test1() {
        //save new user in db:
        userDao.save(new User("Name1")); //in result in db 1 user
        //set savepoint:
        TransactionStatus status = platformTransactionManager.getTransaction(
                new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
        //do changes in db:
        userDao.save(new User("Name2")); //in result in db 2 users
        //roll back to savepoint:
        platformTransactionManager.rollback(status);
        userDao.findAll(); //in result in db 1 user
    }
于 2015-07-01T09:28:24.237 回答