@DataJpaTest 与 SpringBoot - 不抛出 OptimisticLocking 异常

问题描述

我有使用 postgresql@DataJpaTest。不在内存中。

尝试测试乐观锁定异常。但它没有抛出异常。我对同一个数据库进行了类似的测试,但使用不同的架构,但使用 @SpringBoottest 可以正常工作。有什么想法吗?

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Slf4j
class MyApplicationTest {

@Autowired
private MyRepository myRepository;

@Test
void withOptimisticLockHandling_shouldThrowException() {
     PersonEntity entity = myRepository.saveAndFlush(createPerson(id));

    String id = entity.getId();

    PersonEntity update1 = myRepository.findById(id).get();
    PersonEntity update2 = myRepository.findById(id).get();

    update2.setName("Hello1");
    myRepository.saveAndFlush(update2);

    update1.setName("Hello2");
    assertThrows(ObjectOptimisticLockingFailureException.class,() -> myRepository.saveAndFlush(update1));
}

PersonEntity一个@Version 注释的字段,数据库一个字段 version

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)