Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring集成测试不会从其他模块获取JPA bean_Java_Spring_Spring Boot_Spring Data Jpa_Spring Data - Fatal编程技术网

Java Spring集成测试不会从其他模块获取JPA bean

Java Spring集成测试不会从其他模块获取JPA bean,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,我有一个多模块maven项目。在一个模块中,我有我所有的持久性东西,比如实体和存储库,在另一个模块中,我导入了持久性模块,我想运行一些集成测试。 问题是,我的测试没有拾取存储库,并且由于缺少bean而无法创建上下文。有人能帮我弥补我所缺少的吗 项目结构如下所示 持久性模块 业务逻辑模块 网络模块 在我的业务逻辑模块pom中,我导入了持久性模块 <dependency> <groupId>com.acme</groupId> <arti

我有一个多模块maven项目。在一个模块中,我有我所有的持久性东西,比如实体和存储库,在另一个模块中,我导入了持久性模块,我想运行一些集成测试。 问题是,我的测试没有拾取存储库,并且由于缺少bean而无法创建上下文。有人能帮我弥补我所缺少的吗

项目结构如下所示

  • 持久性模块
  • 业务逻辑模块
  • 网络模块
在我的业务逻辑模块pom中,我导入了持久性模块

<dependency>
    <groupId>com.acme</groupId>
    <artifactId>persistence</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
我所有的测试都有以下注释

@SpringJUnitConfig(DataTestConfig.class)
@DataJpaTest
如果我将所有持久性模块移动到业务模块中,那么测试工作正常,但我希望将它们保留在单独的模块中。 谢谢,我找到了解决办法。 我在测试配置类中添加了
@enableJParepositions
。 这样创建了存储库,现在可以实例化使用存储库的我的服务。 这就是最终集成测试配置的样子

@Configuration
@ComponentScan({"com.acme.persistence", "com.acme.business"})
@EntityScan("com.acme.persistence")
@EnableJpaRepositories(basePackages = "com.acme.persistence")
@EnableAutoConfiguration
public class DataTestConfig {

}

你试过用
@springbootest
注释它们吗?您能否显示一个带有异常和堆栈跟踪的测试用例?这些测试已经用@DataJpaTest进行了注释。为什么我需要添加@SpringBootTest?问题是它无法实例化服务,因为它找不到存储库。错误如下所示:com.acme.business.SomeService中构造函数的参数0需要com.acme.persistence.somerepository类型的bean如果需要集成测试,则需要该注释,为什么不发布代码和错误@cipQuestion如果我要将代码从持久性模块移动到业务模块并运行测试,那么它会工作,但我想将它们分开。有很多代码要发布,我正在努力避免。其思想是所有服务都位于业务模块内,而存储库/实体位于持久性模块内。业务模块导入持久性模块,我想在业务模块上运行一些集成测试,但由于缺少存储库bean,它无法实例化服务。
@Configuration
@ComponentScan({"com.acme.persistence", "com.acme.business"})
@EntityScan("com.acme.persistence")
@EnableJpaRepositories(basePackages = "com.acme.persistence")
@EnableAutoConfiguration
public class DataTestConfig {

}