People, I have my multi-module project, which consists of:
example-backend-development
I configure the entire database configuration in my save properties located inside the resource folder from the domain module. This file contains the following:
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db spring.datasource.username=root spring.datasource.password=myPassword spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.show-sql=true
Therefore, when I boot from my service, I have no problem, because Spring Boot makes the DataSource bean this data. The problem occurs when I want to create a single test inside a domain module.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = DAOConfiguration.class) public class ClientDAOTest { private static final Logger LOGGER = LoggerFactory.getLogger(ClientDAOTest.class); @Autowired private ClientDAO dao; @Test public void testFindClient() { LOGGER.info("Starting find all clients test"); Page<Client> clients = this.dao.findAll(new PageRequest(0, 30, null)); ... } }
Where DAOConfiguration imports beans from PersisteConfiguration:
@Configuration @PropertySource("classpath:persistence.properties") @EnableTransactionManagement public class PersistenceConfiguration { @Autowired private DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setDataSource(this.dataSource); entityManager.setPackagesToScan(new String[] {"com.example.movies.domain"}); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); entityManager.setJpaVendorAdapter(vendorAdapter); return entityManager; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } }
Then, when I try to run testFindAllClient, I have this exception:
... Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] ... 52 common frames omitted 15:23:33.647 [main] DEBUG ostcsDirtiesContextTestExecutionListener - After test class: context [ DefaultTestContext@3e7cddcb testClass = ClientDAOTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [ MergedContextConfiguration@3db6fb5d testClass = ClientDAOTest, locations = '{}', classes = '{class com.example.movies.domain.config.DAOConfiguration}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false].
Which I understand, because I do not start it using Spring Boot and, therefore, DAtaSource is not created. Do you have any idea how I can solve this? Thanks in advance!