'Why JpaRepository doesn't commit to database when called from @SpringBootTest?

When repository.save(t) is called from my service, which is in turn called from my controller, all works just fine, and the object is inserted into the database table; But, when the service is called from my test class, Hibernate returns the created object but does not really flush the transaction into the database. I have tried using @Transactinal and @Commit in my test class and also on my @Test methods, but no difference in the result. I have also tried other solutions which involve using org.springframework.test.context.transaction.TestTransaction class, but any method call on this class throws an exception. this is my super class for test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class QaApplicationTest {
    protected abstract void initializeTest() throws Exception;

    protected abstract void cleanupTestEffects() throws Exception;
}

And this is my concrete test class:

public class RequestControllerTest extends QaApplicationTest {
    @Autowired
    private SiteService siteService;

    @Autowired
    private RequestService requestService;

    @Test
    @Transactional
    public void givenObject_whenInsertToDB_thenCreated() throws Exception{
        Site siteObject = siteService.save(siteObject); //Here I need a commit.

        Request request = new Request(site.getId());
        Request savedRequest = requestService.save(request); //Here database returns "Parent Key Not Found" error.

        Assertions.assertTrue(savedRequest.getId()>0);
        
    }
}

I know the @Transactional on test methods are used to roll back all the changes made inside the method, however, In my case, the changes are not even committed in the first place. And I have used @org.springframework.transaction.annotation.Transactional which is the correct annotation. I don't know which part I am doing wrong! Any idea?



Solution 1:[1]

My colleague found the issue; we had used a third-party library (Camunda) that had enabled batch-insert on Hibernate. So by disabling the batch operation the issue was resolved and the insert is actually taking place now. Not sure, why we faced this only in Spring Test and not in the main application though. if anyone has a comment, we appreciate it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mostafa.S