Spring Download @WebIntegrationTest and TestRestTemplate - Can rollback transactions be performed?

I have a Spring boot application with Spring Data Rest and I use @WebIntegrationTest along with TestRestTemplate in my integration tests. The base class for tests looks something like this:

 @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles(profiles = "test") @SpringApplicationConfiguration(classes = Application.class) @Transactional @TransactionConfiguration @WebIntegrationTest("server.port: 0") public abstract class IntegrationTest { ... } 

I tested creating an object using TestRestTemplate to execute a POST request to a resource. The problem is that the transaction that stores the object in the database is not rolled back, even if my tests are configured as transactional, so the object remains in the database after the test. I understand that since the transaction that is rolled back in the test is not the same as for the object.

Now my question is: is there a way to roll back transactions caused by queries executed using RestTemplate in the testing method?

+6
source share
1 answer

Is there a way to roll back transactions caused by queries made using RestTemplate in a test method?

No. Unable to roll back transactions managed by your deployed application.

When you comment on your test class using @WebIntegrationTest and @SpringApplicationConfiguration , Spring Boot launches the built-in Servlet container and deploys your application in it. So, in this sense, your test and application work in two different processes.

The Spring TestContext Framework manages Test Transactions . Thus, the presence of @Transactional in your test class affects only local transactions controlled by testing, and not different processes.

As mentioned earlier, the crawl will consist of resetting the database after the test completes. You have several options for this. For more information, see Executing SQL Scripts in the Reference Guide.

Hello,

Sam (author of Spring TestContext Framework)

+17
source

Source: https://habr.com/ru/post/985345/


All Articles