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;
}
我希望能夠運行以下測試:
@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?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的java测试spring cloud_java – 从Spring引导单元测试中排除Spring Cloud Config Server的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017内存品牌大揭秘:稳定VS速度,到
- 下一篇: 华硕M4A77TD主板内存插槽设计,性能