简体   繁体   English

在没有@Repository实现的情况下使用@NoRepositoryBean运行Spring集成测试

[英]Running Spring integration tests with @NoRepositoryBean with no @Repository implementation

I have a situation where I have a few modules, with the primary module being datastore agnostic. 我遇到的情况是我有几个模块,而主要模块与数据存储区无关。 There are child modules that depend on the primary module with repository implementations for JPA and Mongo. 有一些子模块依赖于主模块以及JPA和Mongo的存储库实现。

  • Core-Module - All business logic, @NoRepositoryBean interfaces Core-Module -所有业务逻辑, @NoRepositoryBean接口
  • Core-Mongo - Dependent on Core-Module with Mongo repositories/domain Core-Mongo依赖于具有Mongo存储库/域的Core-Module
  • Core-JPA - Dependent on Core-Module with JPA repositories/domain Core-JPA依赖于具有JPA存储库/域的Core-Module

Essentially, what I'd like to do is have integration tests in Core-Module use an H2 database to perform the tests and not be dependent on a @Repository implementation. 本质上,我想做的是在Core-Module进行集成测试,使用H2数据库执行测试,而不依赖于@Repository实现。

I realize I could make test-only @Repository interfaces, and that may be the route I have to go, but I'm wondering if there's a simpler way. 我意识到我可以制作仅测试的@Repository接口,这也许是我必须走的路,但是我想知道是否有更简单的方法。 Essentially, is there a way to "instantiate" a @NoRepositoryBean in tests or some other simple way to do this without having to declare test only repositories? 本质上,是否有一种方法可以在测试中“实例化” @NoRepositoryBean或其他一些无需声明仅测试存储库的简单方法?

I just checked this interest question. 我刚刚检查了这个兴趣问题。 I marked in my test project UserRepository by @NoRepositoryBean , after that in test i unable to autowired it. 我在测试项目UserRepository@NoRepositoryBean ,之后在测试中我无法对其进行自动@NoRepositoryBean But in documentation spring-docs i found java creation repositories process description, and it's works perfect. 但是在文档spring-docs中,我发现了Java创建存储库过程描述,并且它的工作原理很完美。 Just before test create required repositories. 在测试之前,创建所需的存储库。 Piece of test code example: 一段测试代码示例:

  private UserRepository userRepository;

  @Autowired
  private EntityManager entityManager;

  @Before
  public void init() {
    JpaRepositoryFactory factory = new JpaRepositoryFactory(entityManager);
    userRepository = factory.getRepository(UserRepository.class);
  }

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

相关问题 使用Spring Boot运行集成测试(NoSuchBeanDefinitionException) - Running integration tests with Spring boot (NoSuchBeanDefinitionException) Spring JPA NoRepositoryBean查询 - Spring JPA NoRepositoryBean Queries Spring Integration JPA存储库测试无法正常工作 - Spring integration jpa repository tests don't work as it should 模拟服务,注入存储库和映射器。 Spring 中的集成测试 - Mocking Service, Injecting repository & mapper. Integration Tests in Spring Spring 数据存储库集成测试在本地通过但在 Jenkins 服务器上失败 - Spring data repository integration tests pass locally but fail on Jenkins servers UTF-8 编码未设置为 Spring 集成测试运行与 Maven 在 ZAEA23489CE3AAA9B43006EBB28E0ZCD - UTF-8 encoding not set for Spring integration tests running with Maven on Windows 使用gradle运行spring-boot REST服务的集成测试 - Running integration tests for a spring-boot REST service using gradle 运行集成测试时禁用 Spring Cloud Sleuth? - Disable Spring Cloud Sleuth when running Integration Tests? @NoRepositoryBean与@Repository的非托管类型错误 - @NoRepositoryBean vs @Repository for not a managed type error 使用profile进行Spring集成测试 - Spring integration tests with profile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM