The*_*ect 2 testing spring integration-testing spring-boot
我在类路径中有一个spring-boot
项目spring-boot-starter-web
和spring-boot-starter-data
依赖项。
compile "org.springframework.boot:spring-boot-starter-web:1.0.2.RELEASE"
compile "org.springframework.boot:spring-boot-starter-data-jpa:1.0.2.RELEASE"
Run Code Online (Sandbox Code Playgroud)
现在,我只想测试spring-data-jpa
相关的类。为此,我只想spring-boot
进行自动配置spring-boot-starter-data-jpa
。但是,如果我@EnableAutoConfiguration
在@Configuation
用于测试的课堂上进行,则spring-boot
尝试为spring-boot-starter-web
和spring-boot-starter-data-jpa
.
@SpringApplicationConfiguration(classes = {DataConfig.class})
public class PersonRepositoryTests extends AbstractJUnit4SpringContextTests {
@Autowired
PersonRepository personRepository;
@Test
public void testSaveWithNameNull() {
/* ... */
}
}
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackageClasses = {RepoPackage.class})
@EntityScan(basePackageClasses = {DomainPackage.class})
@EnableTransactionManagement
public class DataConfig {
}
Run Code Online (Sandbox Code Playgroud)
如何配置 spring boot 使其仅出于测试目的自动配置特定依赖项?
在我的上下文中,我只想spring-boot
自动配置spring-data-jpa
相关配置而忽略web
相关配置。
或者,还有其他更好的方法可以在 spring-boot 中设置这种类型的测试配置吗?
即使上面的答案是正确的,还有一个额外的评论。
该@EnableAutoConfiguration
注释开始自动配置所有其发现的类路径。您可以通过排除某些自动配置类来排除事物,例如:
@AutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
Run Code Online (Sandbox Code Playgroud)
同样,您可以通过@Import
自动配置类自动配置一些功能。喜欢:
@Import({DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)
我的真实世界示例:
@EnableAutoConfiguration
找到了我的主要应用程序@Configuration
类@Import
在由保护的模块配置中添加了@Conditional
归档时间: |
|
查看次数: |
2159 次 |
最近记录: |