我試着爲spock中的CompletableFuture創建存根或模擬。我的方法被稱爲異步並返回CompletableFuture。在spock方法中總是返回null。怎麼了?Spock中的Mock CompletableFuture
public class ProductFactory() {
@Autowired
ProductRepository repository;
public Product create(String name) {
this.checkProductExists(name);
}
public CompletableFuture<Boolean> checkProductExists(String name) {
//call repository and return
boolean result = this.repository.checkProductExists(name);
return CompletableFuture.completedFuture(result)
}
}
class ProductFactorySpec extends Specification {
ProductRepository repository = Mock(ProductRepository)
ProductFactory factory = new ProductFactory(repository)
def "When i decide create new product"() {
def future = CompletableFuture.completedFuture(true)
when:
repository.checkProductExists("Fake string") >> future
future.get() >> true
def result = this.factory.create(fakeOfferData())
then:
result instanceof Product
}
}
更新代碼,它沒有完成。
爲什麼不包含在這個問題你的代碼,以便它更容易讓我們看到什麼正在進行 –
' public void create(String name){ this.checkProductExists(name); } 公共CompletableFuture checkProductExists(字符串名稱){// imeplementation }' 這樣的事情。 –
Matrix12
請用完整的代碼編輯您的問題。 –