【发布时间】:2014-05-25 15:35:01
【问题描述】:
我有一个 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"
现在,我只想测试spring-data-jpa 相关的类。为此,我希望 spring-boot 仅对 spring-boot-starter-data-jpa 进行自动配置。但是,如果我在用于测试的@Configuation 类中执行@EnableAutoConfiguration,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 {
}
如何配置 Spring Boot,以便仅出于测试目的自动配置特定依赖项?
在我的上下文中,我希望 spring-boot 仅自动配置 spring-data-jpa 相关配置而省略 web 相关配置。
或者,还有其他更好的方法可以在 spring-boot 中设置这种类型的测试配置吗?
【问题讨论】:
-
我认为您的 gradle 配置中有错字(Spring Boot 4.0.5 尚不可用,1.5.2 也不可用)。
-
谢谢。我在我的项目中使用了变量,在这里提问时我打错了。
-
如果您使用 Spring Boot gradle 插件,则根本不需要指定版本。
标签: spring testing integration-testing spring-boot