当前位置: 首页 > 知识库问答 >
问题:

Spock中预定义的模拟响应

何浩荡
2023-03-14
public class Basket {
  private WarehouseIneventory warehouseInventory;
  private ShippingCalculator shippingCalculator;
  protected Map<Product, Integer> contents = new HashMap<>();
  ...

  public void addProduct(Product product) {
    addProduct(product, 1);
  }

  public void addProduct(Product product, int times) {
    if (contents.containsKey(product)) {
      int existing = contents.get(product);
      contents.put(product, existing + times);
    } else {
      contents.put(product, times);
    }
  }

  public Boolean canshipCompletely() {
    if(warehouseInventory.isEmpty()) return false;

    try {
      for (Entry<Product, Integer> entry : contents.entrySet()) 
        boolean ok = warehouseInventory.isProductAvailable(
                                  entry.getKey().getName(), 
                                  entry.getValue()
                                  );
        if (!ok) {
          return false;
        }
    }
      return true;
    } catch (Exception e) {
      return false;
    }

  ...
}
def "Warehouse is queried for each product"() {
    given: "a basket, a TV and a camera"
    Product tv = new Product(name:"bravia",price:1200,weight:18)
    Product camera = new Product(name:"panasonic",price:350,weight:2)
    Basket basket = new Basket()

    and: "a warehouse with limitless stock"
    WarehouseInventory inventory = Mock(WarehouseInventory)
    basket.setWarehouseInventory(inventory)

    when: "user checks out two products"
    basket.addProduct tv
    basket.addProduct camera
    boolean readyToShip = basket.canShipCompletely()

    then: "order can be shipped"
    readyToShip
    2 * inventory.isProductAvailable( _ , _) >> true
    0 * inventory.preload(_ , _)
}
def "Warehouse is queried for each product"() {
    given: "a basket, a TV and a camera"
    Product tv = new Product(name:"bravia",price:1200,weight:18)
    Product camera = new Product(name:"panasonic",price:350,weight:2)
    Basket basket = new Basket()

    and: "a warehouse with limitless stock"
    WarehouseInventory inventory = Mock(WarehouseInventory)

    // ******** Move mock predefined response here  **********
    inventory.isProductAvailable( _ , _ ) >> true         
    basket.setWarehouseInventory(inventory)

    when: "user checks out two products"
    basket.addProduct tv
    basket.addProduct camera
    boolean readyToShip = basket.canShipCompletely()

    then: "order can be shipped"
    readyToShip
    2 * inventory.isProductAvailable( _ , _)
    0 * inventory.preload(_ , _)
}

我不明白为什么不能将模拟的预定义行为移到and:块中。

共有1个答案

陈允晨
2023-03-14

请参阅文件

当模仿和截断相同的方法调用时,它们必须发生在相同的交互中。特别是,以下mockito风格的stubbing和mocking拆分为两个单独的语句将不起作用:

setup:
subscriber.receive("message1") >> "ok"

when:
publisher.send("message1")

then:
1 * subscriber.receive("message1")

正如在Where to Declary Interactions中解释的那样,receive调用将首先与then:块中的交互进行匹配。由于该交互没有指定响应,因此将返回方法返回类型的默认值(在本例中为null)。(这只是斯波克宽大嘲讽的另一个方面。)。因此,setup:block中的交互将永远不会有机会匹配。

 类似资料:
  • 这里,在引擎盖下调用。因此,我需要我的模拟实例返回(指示失败的auth),或者在发生意外情况时抛出。 你知道我该怎么做吗?

  • 我在spring boot应用程序中用groovy(spock)编写了一个集成测试。其中一个应用程序bean称为,它具有以下方法: 在类中还有其他方法。在我的集成测试中,我只想模拟该特定方法的响应。以以下方式: 我希望进行其他验证,但不是这次。基本上,我想实现这一点,但与斯波克。我想取消执行方法

  • 所以我想做一些事情 但我得到了空异常

  • 我有一个用注释的Groovy类,因此它得到一个私有的最终字段,我想测试它的用法。我想继续使用,而不是为了启用测试而进一步公开字段。 我正在使用Spock1.0编写测试,并尝试使用Spock的集成、模拟和截尾功能来完成测试。全局截尾可以帮助我截取调用以获得实际的实例,因此我目前的猜测是: 有趣的是,拦截实际上起作用了,确认类实际上获得了名为“dummy”的类型“logger”的对象

  • 预先定义的虚拟服务器 FreeRADIUS包括站点可用子目录下的虚拟服务器。有些可以按原样使用,而有些则是用于特殊要求的模板。以下是一些虚拟服务器: buffered-sql:此虚拟服务器用于克服大型SQL数据库(type = detail)的速度限制。 copy-acct-to-home-server:此虚拟服务器可用作模板,用于在两个位置记录一个计费请求(type = detail)。 coa

  • 我在micronaut中有以下接口来执行HTTP POST请求: 我有一个调用接口的类: 我想在我的spock测试中模拟/存根API调用,我尝试了以下方法: 然而,我得到的错误: