Spring JPA 测试 - 回滚
Spring JPA tests - rollback
我正在尝试测试 DAO 层。我将我的应用程序连接到本地 postgresql 服务器。
测试通过并回滚数据(table 在数据库中保持为空)。
当我尝试在测试代码中打印 notificationTerm.getId() 时,它在每次测试执行后递增 1。
但是哪一行代码回滚了数据?
@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);
}
}
当您使用 @DataJpaTest
时,这是默认行为。
Spring Boot Data JPA Test 正在执行测试方法并在每个测试方法之后进行回滚。
我正在尝试测试 DAO 层。我将我的应用程序连接到本地 postgresql 服务器。
测试通过并回滚数据(table 在数据库中保持为空)。
当我尝试在测试代码中打印 notificationTerm.getId() 时,它在每次测试执行后递增 1。
但是哪一行代码回滚了数据?
@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);
}
}
当您使用 @DataJpaTest
时,这是默认行为。
Spring Boot Data JPA Test 正在执行测试方法并在每个测试方法之后进行回滚。