5

我正在尝试使用 spock where 块运行 Grails 3 服务集成测试。但是,我得到一个空指针异常(见下文)。如果我将它作为连续的 then/when 块运行它。知道我错过了什么吗?

@Integration
@Rollback
class PersistenceAutoCompleteTermServiceIntegrationSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    @Autowired
    PersistenceAutoCompleteTermService persistenceAutoCompleteTermService

    @Unroll
    void "fetchAllLikeTerm tests"() {

        setup:
            new PersistenceAutoCompleteTerm(name: 'Milkshake').save()
            new PersistenceAutoCompleteTerm(name: 'Oat Milk').save()
            new PersistenceAutoCompleteTerm(name: 'Special K').save()

        when:
            List<String> names = persistenceAutoCompleteTermService.fetchAllLikeTerm(term)

        then:
            names.size() == expected_result

        where:
        term        | expected_result
        "Milk"      | 2
        "milk"      | 2
    }
}

失败

java.lang.NullPointerException
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
    at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:64)
    at com.shoptimix.api.persistence.PersistenceAutoCompleteTermServiceIntegrationSpec.fetchAllLikeTerm tests(PersistenceAutoCompleteTermServiceIntegrationSpec.groovy)

但是,这有效:

@Unroll
void "fetchAllLikeTerm tests"() {

    setup:
        new PersistenceAutoCompleteTerm(name: 'Milkshake').save()
        new PersistenceAutoCompleteTerm(name: 'Oat Milk').save()
        new PersistenceAutoCompleteTerm(name: 'Special K').save()

    when:
        List<String> names = persistenceAutoCompleteTermService.fetchAllLikeTerm("Milk")

    then:
        names.size() == 2

    when:
        names = persistenceAutoCompleteTermService.fetchAllLikeTerm("milk")

    then:
        names.size() == 2
}
4

0 回答 0