Grails spock服务测试没有调用服务方法

时间:2015-02-20 18:03:21

标签: spock grails-2.3

我在使用Grails 2.3.7的服务的spock测试中看到了一些奇怪的行为。

此测试工作正常:

void "create spot order"() {
    given:
    def createOrderCommand = newCreateOrderCommand(OrderType.S)

    when:
    def orderId = service.createOrder(createOrderCommand, user).id.toInteger()

    then:
    Order.count() == 1

    when:
    def order = service.orderById(orderId)

    then:
    // a bunch of assertions
}

此测试也可以正常使用:

void "create command with invalid order id"() {
    when:
    service.commandForOrderId(999)

    then:
    def exception = thrown(CreateOrderException)
    exception.key == "orderService.invalid.order.id"
}

然而,这个测试失败了 - 我在开头的commandForOrderId设置了一个断点,它永远不会被击中。 command为null(这是测试失败的地方,在检查命令为null的行上),这将永远不会从此服务方法返回:

void "create spot command"() {
    given:
    def createOrderCommand = newCreateOrderCommand(OrderType.S)

    when:
    def order = service.createOrder(createOrderCommand, user)

    then: 
    Order.count() == 1

    when:
    def orderId = order.id.toInteger()
    def command = service.commandForOrderId(orderId)

    then:
    command
    // a bunch more assertions
}

我尝试使用事务静态字段从服务中删除@Transactional注释,以及不使用这些注释。我也尝试将服务方法更改为闭包,一切都没有运气。

1 个答案:

答案 0 :(得分:1)

我将服务方法更改为闭包,清除了我的目标目录,做了我所知道的各种清理(在GGTS和grails命令中),并且服务方法在测试中开始受到影响。但是,我仍然不确定这个错误的实际原因。