javascript
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;
}
我希望能夠運(yùn)行以下測試:
@RunWith(SpringRunner.class)
@SpringBootTest
public class S2Test {
@Autowired
S2 s;
@Test
public void t2() {
System.out.println(s.s1.i);
}
}
我的問題是,因?yàn)槲蚁雴为?dú)測試S2類,因?yàn)樗褂?#64;Autowired我必須在我的測試中有一個Spring上下文,但是當(dāng)Spring上下文啟動時它會嘗試創(chuàng)建包含bean的所有3個bean與@Value.由于此值僅可從Spring Cloud Config Server獲得,因此無法創(chuàng)建上下文,從而產(chǎn)生錯誤:org.springframework.beans.factory.BeanCreationException:創(chuàng)建名為’serviceConfig’的bean時出錯:注入自動連接的依賴項(xiàng)失敗;嵌套異常是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?
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的java测试spring cloud_java – 从Spring引导单元测试中排除Spring Cloud Config Server的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017内存品牌大揭秘:稳定VS速度,到
- 下一篇: 华硕M4A77TD主板内存插槽设计,性能