【问题标题】:Spring Boot + CloudFoundry + Test Unit issueSpring Boot + CloudFoundry + 测试单元问题
【发布时间】:2016-11-28 15:09:22
【问题描述】:

我想为我的应用程序进行单元测试。我正在使用 Spring Boot 和 CloudFoundry。 当我在没有云连接的情况下启动“测试文件”时,我的日志中出现错误: “原因:org.springframework.cloud.CloudException:找不到合适的云连接器”

我尝试使用:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT, properties = {
        "spring.cloud.config.enabled:false" })

如何为单元测试排除/关闭云?

谢谢!

【问题讨论】:

  • 你找到解决办法了吗?

标签: spring junit spring-boot cloud-foundry


【解决方案1】:

我通常在特定于 CF 的配置上添加 @Profile("cloud")cloud 配置文件将自动应用到 CF 环境中。

【讨论】:

  • 谢谢。但在我的情况下,我想在单元测试开始时跳过特定于 CF 的配置。你能帮帮我吗?
【解决方案2】:

您的应用程序是通过 Boot 启动器获取 Spring Cloud 连接器(例如org.springframework.boot:spring-boot-starter-cloud-connector),还是您对连接器库有明确的依赖关系(例如org.springframework.cloud:spring-cloud-spring-service-connector)?

如果您使用的是引导启动器,则不应启用云连接器自动配置,除非cloud Spring profile is active。确保在运行测试时该配置文件未处于活动状态。

如果您想在测试中明确禁用云连接器自动配置,proper syntax 将是:

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT, properties = {
    "spring.cloud.enabled=false" })

【讨论】:

    【解决方案3】:

    在本地环境中运行时会出现此问题 代码中的问题

    CloudConfig extends ApplicationCloudConfig {
            // local and cloud configuration code seperated with @Profile.
        }
    

    解决的代码

        class Config {
        //... some config code ...
            @Profile("cloud")
            static class CloudConfig extends  ApplicationCloudConfig {
            //cloud config related code 
            }
    
            @Bean
            @Profile({"local"})
            public DataSource dataSource() {
              //data source config
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 2015-12-20
      • 1970-01-01
      • 2018-10-10
      • 2016-03-15
      • 2017-02-08
      • 2020-08-08
      • 2020-06-25
      相关资源
      最近更新 更多