I have been struggling for a couple of days with spring testing. We are using maven-failsafe-plugin and using docker containers for our two specific databases.
But when we include the non-primary datasource configuration, no tests are run.
So config is (docker-maven-plugin sets the ip/host system properties for us):
metadata:
datasource:
url: jdbc:mariadb://${docker.mariadb.ip}:${docker.mariadb.port}/METADATA
password: m_user
username: m_pswd
reporting:
datasource:
jdbcUrl: jdbc:vertica://${docker.vertica.ip}:${docker.vertica.port}/REPORTING?allowMultiQueries=true
username: v_user
password: v_pswd
We have two database configuration classes.
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"com.company.application"}
)
public class MariadbConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix = "metadata.datasource")
public DataSourceProperties reportingDataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "metadata.datasource")
public HikariDataSource dataSource(DataSourceProperties mariadbDataSourceProperties) {
return (HikariDataSource) mariadbDataSourceProperties.initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
...
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory")
EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Primary
@Bean(name = "transactionTemplate")
public TransactionTemplate transactionTemplate(@Qualifier("transactionManager")
PlatformTransactionManager transactionManager) {
return new TransactionTemplate(transactionManager);
}
}
And
@Configuration
public class VerticaConfiguration {
@Bean("verticaDataSourceProperties")
@ConfigurationProperties(prefix = "reporting.datasource")
public DataSourceProperties verticaDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "verticaDataSource")
@ConfigurationProperties(prefix = "reporting.datasource")
public HikariDataSource verticaDataSource() {
return (HikariDataSource) verticaDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean(name = "verticaJdbcTemplate")
public JdbcTemplate verticaJdbcTemplate() {
return new JdbcTemplate(verticaDataSource());
}
}
So our test class is:
@RunWith(SpringRunner.class)
@ActiveProfiles("itest")
@ContextConfiguration(classes={MariadbConfiguration.class, VerticaConfiguration.class})
@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class ApplicationIT {
@Autowired
DataSource mariadb;
@Qualifier("verticaDataSource")
@Autowired
DataSource vertica;
@Test
public void testApplicationLoaded() {}
}
So if we mvn verify
this no tests are run
[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ our-application ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
BUT if we change the @ContextConfigutation
to not include vertica
@ContextConfiguration(classes={MariadbConfiguration.class})
Then the test runs (and fails with bean initiasation error as expected)
[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ our-application ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
...
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.81 s <<< FAILURE! - in com.company.application.test.ApplicationIT
[ERROR] testApplicationLoaded Time elapsed: 0.001 s <<< ERROR!
org.springframework.beans.factory.UnsatisfiedDependencyException: ...
...
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
If we change the @ContextConfigutation
to not include mariadb
@ContextConfiguration(classes={VerticaConfiguration.class})
Then no tests are run.
Just to be clear, I am not asking about the error.
The question is why does including the VerticaConfiguration.class
mean no tests are run? Seems very weird. Application itself is fine, this is just a testing issue.
Versions:
- java: 8
- spring boot: 2.2.6
- spring framework: 5.2.5
- junit: 4.12
Part of our problem is we kind of inherited this project. So we use spring without properly understanding how it all hangs together.