【问题标题】:Transactional tests with spring boot and hibernate jpa repository使用 spring boot 和 hibernate jpa 存储库进行事务测试
【发布时间】:2019-05-29 02:33:31
【问题描述】:

我想编写事务性单元测试,以便在方法完成时回滚更改,但由于使用休眠和 JPA 存储库,我遇到了问题。由于某些原因,我在尝试使用 @Transactional 注释 @Test 方法时得到 UnsupportedOperationException
这是我正在尝试测试孤儿删除逻辑的代码,一切正常,但我不希望这些实体在方法完成后出现在数据库中。

@RunWith(SpringRunner.class)
@SpringBootTest
public class NotificationGroupServiceTest {

  @Autowired
  private NotificationGroupService notificationGroupService;

  private NotificationGroupEntity groupEntity;
  private Long groupId;
  private NotificationCriterionEntity notificationCriterionEntity;
  private HistoricalNotificationEntity historicalNotificationEntity;

  @Before
  public void initializeEntities() {
    groupEntity = new NotificationGroupEntity();

    groupEntity = notificationGroupService.createOrUpdate(groupEntity);
    groupId = groupEntity.getId();

    notificationCriterionEntity = new NotificationCriterionEntity();
    historicalNotificationEntity = new HistoricalNotificationEntity();
    notificationCriterionEntity.setNotificationGroupId(groupId);
    historicalNotificationEntity.setNotificationGroupId(groupId);
    groupEntity.setHistoricalNotifications(Arrays.asList(historicalNotificationEntity));
    groupEntity.setActiveNotificationsList(Arrays.asList(notificationCriterionEntity));
  }

  @Test
  public void testOrphanRemoval() {
    notificationGroupService.createOrUpdate(groupEntity);

    Optional<NotificationGroupEntity> optionalNotificationGroupEntity =
        notificationGroupService.findById(groupId);

    Assert.assertTrue(optionalNotificationGroupEntity.isPresent());

    groupEntity = optionalNotificationGroupEntity.get();

    Assert.assertEquals(1, groupEntity.getActiveNotificationsList()
        .size());
    Assert.assertEquals(1, groupEntity.getHistoricalNotifications()
        .size());
    Assert.assertEquals(groupEntity.getActiveNotificationsList()
        .get(0)
        .getNotificationGroupId(), groupId);
    Assert.assertEquals(groupEntity.getHistoricalNotifications()
        .get(0)
        .getNotificationGroupId(), groupId);

    groupEntity.setActiveNotificationsList(Arrays.asList());
    groupEntity.setHistoricalNotifications(Arrays.asList());

    notificationGroupService.createOrUpdate(groupEntity);

    optionalNotificationGroupEntity =
        notificationGroupService.findById(groupId);

    Assert.assertTrue(optionalNotificationGroupEntity.isPresent());

    groupEntity = optionalNotificationGroupEntity.get();

    Assert.assertEquals(0, groupEntity.getActiveNotificationsList()
        .size());
    Assert.assertEquals(0, groupEntity.getHistoricalNotifications()
        .size());
  }
}

【问题讨论】:

  • 我也有同样的问题。你解决了吗?

标签: java spring hibernate spring-boot transactional


【解决方案1】:

使用@After 注释来指示要在每个@Test 之后运行的方法。

像这样的全套注释是:

  • @BeforeClass - 在所有 @Tests 运行之前
  • @Before - 在每个 @Test 运行之前
  • @After - 在每个 @Test 运行之后
  • @AfterClass - 毕竟@Tests 都运行了

如果您询问如何将特定的拆卸方法关联到特定的 @Test 方法,则无需注释:只需在 finally 中的测试方法末尾调用它即可。

你的测试类应该是:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration("classpath:/spring-beans.xml")
public class NotificationGroupServiceTest {
....
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 2015-06-23
    • 2019-01-23
    • 2013-09-14
    • 1970-01-01
    • 2023-03-03
    • 2018-05-25
    • 2018-08-06
    相关资源
    最近更新 更多