Spring boot test @Transactional not saving

后端 未结 5 1384
别那么骄傲
别那么骄傲 2020-12-17 21:24

I\'am trying to do a simple Integration test using Spring Boot Test in order to test the e2e use case. My test does not work because I\'am not able to make the repository sa

5条回答
  •  旧巷少年郎
    2020-12-17 22:02

    Spring for saving entity requires transaction. But until transaction has been commited changes not be visible from another transaction.

    Simplest way is call controller after commit transaction

    @Test
    @Transactional
    public void contextLoads() {
        Person person = personService.createPerson(1, "person1");
        Assert.assertNotNull(person);
    
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            @Override
            public void afterCommit() {
                ResponseEntity persons = restTemplate.getForEntity("/persons", Person[].class);
            }
        });        
    }
    

提交回复
热议问题