简体   繁体   English

SpringBoot、JPA 和 Hibernate 实体层的单元测试

[英]SpringBoot, JPA and Hibernate Unit-Test for Entities layer

I'm implementing a project using SpringBoot, JPA and Hibernate.我正在使用 SpringBoot、JPA 和 Hibernate 实现一个项目。

I implemented the DB entities layer with JPA repository.我使用 JPA 存储库实现了 DB 实体层。

I'm interested to understand the best practice to write unit-tests for this layer.我有兴趣了解为这一层编写单元测试的最佳实践。

  • Point number one: for this layer, from your point of view, it's necessary to use an integrated DB or it' necessary to mock using, for example, Mockito?第一点:对于这一层,从您的角度来看,有必要使用集成数据库还是必须使用例如 Mockito 进行模拟?
  • My idea, for this layer, it's, for example, to test the entity structure: check fields validation for example, insert and retrieve some data.我的想法,对于这一层,例如,测试实体结构:例如检查字段验证,插入和检索一些数据。 In this way, I think I could cover the tests for this entire data-layer.通过这种方式,我想我可以涵盖整个数据层的测试。

I'm trying to understand these best practices and, in the mean time, I tried to write a first example of the test:我试图了解这些最佳实践,同时,我尝试编写测试的第一个示例:

@ActiveProfiles("test")
@DisplayName("Test Item JPA Entity")
@DataJpaTest
@AutoConfigureTestDatabase( replace = AutoConfigureTestDatabase.Replace.NONE )
public class ItemEntityTest {

    @Autowired
    MyEntityRepository repo;

    @Test
    @Transactional
    public void testEntityCreation() {

        Entity e = new Entity();
        e.setMyField1("A");
        e.setMyField1("A");
        //e.setMandatoryField("C")

        repo.save(e);

    }
}

Unfortunately, In this case, I notiest that the fields validation is not applied (@NotNull or @NotEmpty, or @Column(nullable=false), etc... If I try to save the entity into my application the validation works fine... the exceptions are raised).不幸的是,在这种情况下,我注意到没有应用字段验证(@NotNull 或@NotEmpty,或@Column(nullable = false)等......如果我尝试将实体保存到我的应用程序中,验证工作正常。 .. 引发异常)。 Why?为什么?

Also some "automatic fields" (for example creation time and last modification time) are not filled.还有一些“自动字段”(例如创建时间和最后修改时间)没有填写。

Is this the correct path?这是正确的路径吗? Ho to test my entities definition?何来测试我的实体定义?

As mentioned in that answer, the problem is that with @DataJpaTest spring will use TestEntityManager and the transactional annotation will override the default auto commit and rollback behaviour by spring boot.如该答案中所述,问题在于@DataJpaTest spring 将使用TestEntityManager并且事务注释将通过 spring 引导覆盖默认的自动提交和回滚行为。

So your test method will pass and assuming the hibernate is the ORM being used here, when the flushing will take place finally, hibernate's pre-insert event will fire and validations will be applied, but your test case will pass till then so it will produce false positive (as the terms used in spring docs)因此,您的测试方法将通过并假设 hibernate 是此处使用的 ORM,当最终进行刷新时,将触发休眠的预插入事件并应用验证,但您的测试用例将通过直到那时它会产生误报(如 spring 文档中使用的术语)

Solution: You would need to inject entity manager in your test and flush manually it so that hibernate pre-insert event triggers before your test completes.解决方案:您需要在测试中注入实体管理器并手动刷新它,以便 hibernate 在测试完成之前触发预插入事件。

@ActiveProfiles("test")
@DisplayName("Test Item JPA Entity")
@DataJpaTest
@AutoConfigureTestDatabase( replace = AutoConfigureTestDatabase.Replace.NONE )
public class ItemEntityTest {

    @Autowired
    MyEntityRepository repo;

    @Autowired
    private TestEntityManager em;

    @Test
    @Transactional
    public void testEntityCreation() {

        Entity e = new Entity();
        e.setMyField1("A");
        e.setMyField1("A");
        //e.setMandatoryField("C")

        repo.save(e);
        
        em.flush();
    }
}

This must trigger your validations on the entity applied, this is documented in spring framework docs .这必须触发您对所应用实体的验证,这在spring 框架文档中有记录。 Please notice that the documentation is about spring framework and uses session factory, but the concept is same请注意,文档是关于 spring 框架并使用 session 工厂,但概念相同

You may check the spring boot docs as well, which points to the spring framework docs for this behaviour.您也可以查看spring 引导文档,该文档指向 spring 框架文档以了解此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM