【问题标题】:Attempting to assert 500 error, but response is null when conditions are met尝试断言 500 错误,但满足条件时响应为空
【发布时间】:2019-08-12 21:03:16
【问题描述】:

当我自动化这个时,我才知道抛出的 500 错误是由服务器给出的。但是在我们的代码级别,我们已经处理了异常和响应 =null

因此,当我尝试使用 response.status==500 进行断言时,它失败了,因为响应完全为空。

package test

import geb.spock.GebReportingSpec
import groovyx.net.http.RESTClient
import net.sf.json.JSON

import org.apache.log4j.Logger
import spock.lang.Specification
import spock.lang.Unroll

class EventsAPITest extends GebReportingSpec{

    static Logger logger = Logger.getLogger("EventsAPITest");

    @Unroll
    def "Call calendar event for Invalid Location id 110 throws groovyx.net.http.HttpResponseException"() {
        given:
        RESTClient restClient = new RESTClient("https://test-api2.club-os.com")
        def response=restClient.get(path: '/authenticate',requestContentType: JSON,query:[
            'username': 'apiuser',
            'password':'apipassword',
        ])
        def authToken=response.data.token
        logger.info"Token:" +authToken
        restClient.defaultRequestHeaders['Authorization'] = authToken
        def locationId=110
        when:
        response=restClient.get(path: '/locations/'+locationId+'/events',requestContentType: JSON, query:[
            'startTimeStartAt': '2018-05-25T17:00:00.000Z',
            'startTimeEndAt':'2018-06-01T17:00:00.000Z',
            'eventTypeId' : 2                      ])
        then:
        thrown groovyx.net.http.HttpResponseException
        logger.info"Response: "+ response
        //  assert response.data.errorMessage=="Error"
        assert  response.status == 500
        //assert    response.status == 500
        //  assert  response.data.errorMessage == "An unknown error occurred"
    }

我希望能够断言 500 错误并将其返回。

【问题讨论】:

    标签: groovy automation spock


    【解决方案1】:

    这不是特定于 spock 的。您的代码引发异常,因此它无法返回值。

    根据 kriegaex 在 cmets 中的建议进行更新。

    通过将thrown groovyx.net.http.HttpResponseException 表达式的结果分配给变量,您可以访问异常的状态。此处记录了此异常的 API:http://javadox.com/org.codehaus.groovy.modules.http-builder/http-builder/0.6/groovyx/net/http/HttpResponseException.html

    您可以直接访问statusCode 属性,并且您也有一个response 属性。这就是您测试条件所需的一切。

    这是生成的代码:

    import geb.spock.GebReportingSpec
    import groovyx.net.http.RESTClient
    import net.sf.json.JSON
    
    import org.apache.log4j.Logger
    import spock.lang.Specification
    import spock.lang.Unroll
    
    class EventsAPITest extends GebReportingSpec{
    
        static Logger logger = Logger.getLogger("EventsAPITest");
    
    @Unroll
        def "Call calendar event for Invalid Location id 110 throws groovyx.net.http.HttpResponseException"() {
            given:
            RESTClient restClient = new RESTClient("https://test-api2.club-os.com")
            def response=restClient.get(path: '/authenticate',requestContentType: JSON,query:[
                'username': 'apiuser',
                'password':'apipassword',
            ])
            def authToken=response.data.token
            logger.info"Token:" +authToken
            restClient.defaultRequestHeaders['Authorization'] = authToken
            def locationId=110
            when:
            response=restClient.get(path: '/locations/'+locationId+'/events',requestContentType: JSON, query:[
                'startTimeStartAt': '2018-05-25T17:00:00.000Z',
                'startTimeEndAt':'2018-06-01T17:00:00.000Z',
                'eventTypeId' : 2                      ])
            then:
            //Here, assign the exception to a variable.
            def e= thrown groovyx.net.http.HttpResponseException
            logger.info"Response: "+ e.response
            //  assert response.data.errorMessage=="Error"
            assert  e.response.status == 500
            //assert    response.status == 500
            //  assert  response.data.errorMessage == "An unknown error occurred"
        }
    

    【讨论】:

    • 虽然您的答案看起来是正确的,但更多的解释会很好。我必须从问题和答案中复制两个代码 sn-ps 并将它们进行比较,以注意到您所做的更改。实际上,您正在将 thrown 语句中的异常分配给局部变量并利用其属性。
    • 给大牛的一些提示:(1)你的测试不是浏览器测试,你应该扩展spock.lang.Specification,而不是geb.spock.GebReportingSpec,否则会无缘无故启动浏览器。您使用无头 REST 客户端。 (2) 您应该将groovyx.net.http.HttpResponseException 导入并在测试中使用简单的类名。 (3)在then:块中你不需要assert,只要写一个类似e.message == "An unknown error occurred"的条件。
    猜你喜欢
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    相关资源
    最近更新 更多