使用Spring Boot和Hibernate JPA存储库进行事务测试

时间:2019-01-02 12:29:52

标签: java spring hibernate spring-boot transactional

我想编写事务性单元测试,以便在方法完成时将回滚更改,但是由于使用了休眠和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());
  }
}

1 个答案:

答案 0 :(得分:0)

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

全套注释如下:

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

如果您要问如何将特定的拆解方法与特定的@Test方法相关联,则无需注释:只需在测试方法的末尾最后调用它即可。

您的测试班级应该是:

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