我试着为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
请用完整的代码编辑您的问题。 –