Spring - 使用事务控制器进行事务测试

时间:2016-12-09 06:23:39

标签: java spring testing transactions

我想为我的应用程序编写集成测试,因此我使用@Transactional注释创建了一个测试。我做了一些设置查询,并且在测试中可以看到更改,这是预期的结果。但是,由于我的控制器也是事务性的,因此无法在调用之前看到执行的更改(因为它们未被提交)。

@ContextConfiguration
@SpringBootTest
@AutoConfigureMockMvc
class SpringContexstAwareTest extends Specification {

    @Autowired
    private MyTestRepository testRepository

    def dbSetup() {
        testRepository.save(userWithId1)
    }

    def "some test"() {
        testRepository.findOne(1) //success
        mockMvc.perform(get("/user/1")).andReturn() //fails since there is no user with id 1 (the controller is also transactional)
    }
}

有没有办法让子事务(控制器)在spring测试中看到父事务(测试数据库设置)的变化?

0 个答案:

没有答案
相关问题