Home > Mobile >  Spring JPA tests - rollback
Spring JPA tests - rollback

Time:10-28

I'm trying to test DAO layer. I connected my application to local postgresql server.

Test is passed and it rollbacks the data (table stays empty in the database).

And when I try to print notificationTerm.getId() in the test code, it increments by 1 after every test execution.

But which line of code makes rollback of the data?

@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class DAOIntegrationTests {

    @Autowired
    private TestEntityManager entityManager;

    @Test
    public void testNotificationTermFakeDAO() {
        NotificationTerm notificationTerm = new NotificationTerm();
        notificationTerm.setDays(365);

        entityManager.persist(notificationTerm);
        entityManager.flush();

        NotificationTerm foundNotificationTerm = entityManager.find(NotificationTerm.class, notificationTerm.getId());

        assertEquals(notificationTerm.getDays(), 365);
    }

}

CodePudding user response:

As you use @DataJpaTest this is the default behavior.

Spring Boot Data JPA Test is executing the test method and does a rollback after each test method.

  • Related