Java 运行域';在具有@SpringApplicationConfiguration的多模块项目中的s测试不会引发任何此类定义异常?
我的多模块项目结构如下:Java 运行域';在具有@SpringApplicationConfiguration的多模块项目中的s测试不会引发任何此类定义异常?,java,spring,spring-boot,spring-data-jpa,spring-test,Java,Spring,Spring Boot,Spring Data Jpa,Spring Test,我的多模块项目结构如下: project-example (parent) -- project-example-domain: contains DAOs and Services -- project-example-service: contains Controllers 服务模块将域作为依赖项,所以服务知道域,而不是相反 因此,为了测试ClientDAO的完整性(在本例中),我进行了以下测试: @RunWith(SpringJUnit4ClassRunner.class) @Spri
project-example (parent)
-- project-example-domain: contains DAOs and Services
-- project-example-service: contains Controllers
服务模块将域作为依赖项,所以服务知道域,而不是相反
因此,为了测试ClientDAO的完整性(在本例中),我进行了以下测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DAOConfiguration.class)
public class ClientDAOTests {
@Autowired
private ClientDAO dao;
@Test
public void testFindClient() {
...
}
}
当我运行它时,会出现以下异常:
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)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 51 more
最后是我的配置
@Configuration
@PropertySource("classpath:persistence.properties")
@EnableJpaRepositories(basePackages = {"com.example.movies.domain.feature"})
@EnableTransactionManagement
public class DAOConfiguration {
@Autowired
private Environment env;
@Autowired
private DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
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();
}
}
当我从服务运行服务器时,它工作正常,Spring生成了DataSourcebean。但是当我在域中运行测试时,我得到了前面提到的异常
因此,我认为我的问题是我将错误的参数传递给@SpringApplicationConfiguration。我不明白我要通过什么。。。我应该用@springbootplication注释传递给它应用程序类吗?在本例中,这是在服务模块内部,所以域不知道这个类。你能解释一下SpringApplicationConfiguration应该把什么作为使我的测试失败的参数吗?提前谢谢
编辑 这是我的ApplicationRunner类,位于服务层:
@SpringBootApplication
@Import({ServicesConfiguration.class, CustomDataBindingConfiguration.class, CustomMappingConfiguration.class})
@PropertySource(value = {"application.properties"})
public class ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(ApplicationRunner.class, args);
}
}
我解决了同样的问题 问题来自
@SpringApplicationConfiguration(classes=DAOConfiguration.class)
。
您必须引用一个用@EnableAutoConfiguration
注释的类,以便Spring Boot自动注入数据源。(此注释通常继承自@SpringBootApplication
)
您有几个选择:
- 从
@SpringApplicationConfiguration
- 将
添加到@EnableAutoConfiguration
类DAOConfiguration
- 。。。(你明白这一点)