我的设置:
- Java 20
- Sping Boot 3.0.7
原始设置
my.com.app.entities
包中的类Provider
、Manager
和Client
1.类Client
引用Provider
和Manager
- Sping Boot 应用程序类如下所示:
@SpringBootApplication(scanBasePackages = "my.com")
public class AppApplication {
public static void main(String[] args) {
// many things
}
@Bean
WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*").allowedOrigins("*").allowedHeaders("*").exposedHeaders("Content-Disposition");
}
};
}
}
应用程序上下文在测试期间加载没有问题。集成测试运行良好。
新建设置
1.由于代码重组,类Provider
和Manager
被移动到包my.com.core.entities
中(此时,在同一个项目中)。
Client
类在my.com.app.entities
包中继续,引用了Provider
和Manager
没有编译错误(refactorer处理了引用更新),但是在上面的重构之后,集成测试不再运行。对于每个测试,都有一个异常堆栈,如下所示:
java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@3294102e testClass = my.com.app.ClientRepositoryTest, locations = [], classes = [my.com.app.AppApplication], contextInitializerClasses = [], activeProfiles = ["test-integration"], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6c4906d3, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@4d1bf319, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@17bffc17, org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizer@55cb6996, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@4b520ea8, org.springframework.boot.test.context.SpringBootTestAnnotation@91305a8a], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Association 'my.com.app.entity.Client.provider' targets an unknown entity named 'my.com.core.Provider'
Caused by: org.hibernate.AnnotationException: Association 'my.com.app.entity.Client.provider' targets an unknown entity named 'my.com.core.Provider'
我的期待
我希望AppApplication类中的注解通知从my.com
和更低版本扫描将包括类my.com.core.entities.Provider
和my.com.core.entities.Manager
,但它没有。
我的测试具有以下自定义注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ActiveProfiles("test-integration")
@SpringBootTest(classes = AppApplication.class)
@ExtendWith(CleanUpExtension.class)
public @interface IntegrationTest {
}
我所尝试的
我尝试在my.com.core.config
中添加以下配置类:
@Configuration
@ComponentScan
public class CoreConfig {
}
然后我更新了我的测试注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ActiveProfiles("test-integration")
@SpringBootTest(classes = {AppApplication.class, CoreConfig.class})
@ExtendWith(CleanUpExtension.class)
public @interface IntegrationTest {
}
结果
问题仍然存在。
我错过了什么?
先谢谢你了!
1条答案
按热度按时间68de4m5k1#
我找到了一个解决方案,但我不知道为什么有必要。我的
@IntegrationTest
注解最初看起来是这样的:我必须改进它,通过添加
@EntityScan
注解来声明在哪里查找实体。现在我的@IntegrationTest
注解声明如下所示:就是这样!