【发布时间】:2019-02-14 22:48:02
【问题描述】:
默认情况下@DataJpaTest 扫描所有 jpa 存储库和@Entity。就我而言,我有 5 个存储库包和 5 个实体包。例如
com.acme.product.entity 与
com.acme.product.repository
com.acme.users.entity 与
com.acme.users.repository
com.acme.client.entity 与
com.acme.client.repository
等等……
我想在单独的课程中测试每个部分。例如
@RunWith(SpringRunner.class)
@DataJpaTest
//Some configurations to import only product repositories and product entities
public class TestProductRepository {
@Autowired
TestEntityManager entityManager;
}
请注意,我已经配置了 5 个不同的EntityManager,我想导入它们并使用例如TestProductRepository 中的productEntityManager,而不是加载所有存储库/实体的默认TestEntityManager。
非常感谢
【问题讨论】:
-
如果您有存储库,为什么还需要 EntityManager?
-
@SimonMartinelli 覆盖默认值:
TestEntityManager。我只需要一种方法来告诉TestEntityManager使用这些存储库和实体。 -
但是如果你想测试 ProductRepository 你可以注入它并且不需要 entitymanager 因为这是配置的
-
@SimonMartinelli 你是对的,但问题是它会减慢测试速度,因为它会扫描所有存储库和所有实体。
-
我终于找到了一种方法,可以在同一波中覆盖默认的
TestEntityManager,每个@DataJpaTest案例的测试时间除以4。
标签: spring-boot spring-data-jpa spring-data spring-boot-test