【问题标题】:Configure spring-boot to auto-configure for only specific dependencies for testing purpose将 spring-boot 配置为仅针对特定依赖项自动配置以进行测试
【发布时间】:2014-05-25 15:35:01
【问题描述】:

我有一个 spring-boot 项目,在类路径中有 spring-boot-starter-webspring-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 类中执行@EnableAutoConfigurationspring-boot 会尝试自动配置spring-boot-starter-webspring-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


【解决方案1】:

即使上面的答案是正确的,还要多加一条评论。

@EnableAutoConfiguration 注释开始自动配置它在类路径中找到的所有内容。您可以通过排除某些自动配置类来排除事物,例如:

@AutoConfiguration(exclude = {
    DataSourceAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class
})

同样,您可以通过@Import 自动配置类来自动配置部分功能。喜欢:

@Import({DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

我在现实世界中的例子:

  • @EnableAutoConfiguration 位于我的主应用程序 @Configuration 类中
  • 我有一个使用数据库的模块,它可能被禁用,在这种情况下,我根本不想自动配置数据库
  • 所以我从主配置中排除了自动配置
  • 并在由@Conditional 保护的模块配置中添加了@Import

【讨论】:

    【解决方案2】:

    我想你已经拥有了。如果您使用@SpringApplicationConfiguration 并且不指定@WebAppConcfiguration,那么您将得不到 webapp,因此没有 MVC 层。

    【讨论】:

    • 我添加了 thymeleaf 依赖项,当我测试 data-jpa 类时,我遇到了与 thymeleaf 相关的错误,比如模板不可用。
    • 好吧,如果您没有任何模板,那是默认行为。您确定可以在类路径中添加目录吗?
    • 所以即使我收到有关模板的错误,spring-boot 也不会自动配置与 mvc 相关的 bean,对吧?
    • 可能是。这取决于@Conditionals。你为什么要关心呢?
    【解决方案3】:

    @ImportAutoConfiguration 注释,您可以将其用于此目的。

    在groovy中有我的例子,不过你可以简单的重写成Java:

    配置类:

    @Configuration
    @ImportAutoConfiguration(classes = [HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class])
    @EntityScan(basePackages = 'package.with.entities')
    @EnableJpaRepositories(basePackages = 'package.with.repositories')
    class JpaRepositoryConfig {
    
    }
    

    测试类:

    @ContextConfiguration(classes = JpaRepositoryConfig,
    initializers = ConfigFileApplicationContextInitializer.class) //initializer is for loading config from application.yml
    class YourTestClass {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-19
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 2022-09-29
      • 1970-01-01
      相关资源
      最近更新 更多