【问题标题】:What is wrong with my JPA test case? I cannot remove tuples with em.remove()我的 JPA 测试用例有什么问题?我无法使用 em.remove() 删除元组
【发布时间】:2021-08-30 04:52:14
【问题描述】:

我正在尝试测试我的存储库类的删除方法,但我无法通过测试。
这是我的测试代码。

post delete test code

当调用 persistAll() 时,成员被添加到持久化上下文中。接下来,当 flush() 时,由于 Cascade.Persist 选项,100 个帖子被添加到持久化上下文中。

我又调用了count()、delete()和count()来检查post tuple是否从数据库中删除。

这里是 count() 和 delete() 代码。

methods of my repository

我从 JUnit 收到了这个失败消息

test failure message

【问题讨论】:

  • 我通过在persistAll() 方法中的em.flush() 之后添加em.clear() 解决了这个问题
  • 那么你的问题解决了吗?然后请添加您的评论作为答案
  • @SimonMartinelli 我是堆栈溢出的新手。你能告诉我如何添加我的评论作为答案吗?
  • 我明白你说的谢谢!

标签: spring-mvc jpa junit


【解决方案1】:

我通过在 persistAll() 方法中的 em.flush() 之后添加 em.clear() 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多