We recently abstracted our JPA layer out of a few of our microservices that share the same JPAs. We call this application 'core', which is now a jar managed by AWS code artifact. Putting aside that this breaks the microservice pattern/contract of autonomous applications with their own dbs, etc.. When trying to run the @SpringBootTest unit tests in one of the consuming applications, which in this case is a group of spring managed @Scheduled jobs, we are getting this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception with message: dataSource or dataSourceClassName or jdbcUrl is required.
Note that because 'core' connects to multiple schemas in the same database, we created separate JPAConfigs for each schema.
@Bean(name = "primaryDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.primary-datasource")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.second-datasource")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "thirdDataSource")
@ConfigurationProperties(prefix = "spring.third-datasource")
public DataSource thirdDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "fourthDataSource")
@ConfigurationProperties(prefix = "spring.fourth-datasource")
public DataSource fourthDataSource() {
return DataSourceBuilder.create().build();
}
I don't know if that was necessary with:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
</parent>
... but it works in the running application. So I guess the test context is not able to access the data sources in core. I tried adding it to core's test path, i.e. copying src/main/resources over to src/test/resources. No dice.
We fixed this by removing src/test/resources from the consuming application