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 次 |
| 最近记录: |