java测试spring cloud_java – 从Spring引导单元测试中排除Spring Cloud Config Server

鉴于我有以下3豆:

@Component

public class ServiceConfig {

// This value is only available from the Spring Cloud Config Server

@Value("${example.property}")

private String exampleProperty;

public String getExampleProperty() {

return exampleProperty;

}

}

@Component

public class S1 {

int i = 1;

}

@Component

public class S2 {

@Autowired

S1 s1;

}

我希望能够运行以下测试:

@RunWith(SpringRunner.class)

@SpringBootTest

public class S2Test {

@Autowired

S2 s;

@Test

public void t2() {

System.out.println(s.s1.i);

}

}

我的问题是,因为我想单独测试S2类,因为它使用@Autowired我必须在我的测试中有一个Spring上下文,但是当Spring上下文启动时它会尝试创建包含bean的所有3个bean与@Value.由于此值仅可从Spring Cloud Config Server获得,因此无法创建上下文,从而产生错误:org.springframework.beans.factory.BeanCreationException:创建名为’serviceConfig’的bean时出错:注入自动连接的依赖项失败;嵌套异常是java.lang.IllegalArgumentException:无法在字符串值“${example.property}”中解析占位符’example.property’.

My Question is: How are properties read from Spring Cloud Config

Server handled in the application when unit tests are run, observe in

my test i dont even care about the config so I dont want to explicitly

have to set a value in my test just for the context to be started?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值