鑑於我有以下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上下文開始時,它會嘗試創建包含與@Value
的bean的所有3個bean。由於此值僅在Spring Cloud Config Server中可用,因此上下文將無法創建,並出現錯誤:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'example.property' in string value "${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?
你用什麼作爲構建工具? Gradle還是Maven?還有別的嗎? – Pytry
我正在使用Maven – user3139545