[英]Spock Mock String value in class
我正在使用 Spring 並且我想使用 Spock 創建單元測試。 這是我的 class
@Service
public class TestService{
@Value("${test.path:}")
private String path;
}
有沒有辦法在 Spock 測試中模擬這個變量而不運行 spring 上下文?
考慮到您不想設置 Spring(Boot) 測試,請使用構造函數注入注入 value 字段:
@Service
public class TestService{
private String path;
public TestService(@Value("${test.path:}") String path) {
this.path = path;
}
}
...
TestService service = new TestService("testValue");
或使用ReflectionTestUtils設置值:
TestService service = new TestService();
ReflectionTestUtils.setField(service, "path", "somePath");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.