所以这真的取决于你在测试什么。默认情况下,无法在类级别上执行 where。您可以制作注释以从源中提取并替换值。
我所做的是进行了更通用的测试。请注意,参数可以插入到 BDD 方法 cmets 中。您可以将这些设置为自定义参数。唯一的问题是您每次都需要拥有相同数量的元素。
@Unroll
def "Failed request to obtain Oath Token by #failure"(){
given: "a user is attempting to use the 'Obtain OAuth Token' endpoint"
when: "the Content-Type is set to #content_type"
request.setContentType(content_type)
and: "the Accept type is set to #accept"
request.addHeader("Accept", accept)
and: "the user sets the client_id as #clientId"
and: "the user set the client_secret as #clientSecret"
and: "the grant_type is set as #grantType"
and: "the request type is #requestType"
request.setPostBody(ApiRequestBody.oAuthBody(client_id:clientId,client_secret:clientSecret,grant_type:grantType))
response = request.postExecute()
then: "an appropriate status code (#status) is returned"
response.status == status
and: "the error response is '#error_message'"
response.data.errors[0].message == error_message
and: "the error code is #error_code"
response.data.errors[0].code == error_code
and: "the response type is #response_type"
response.contentType == response_type
where:
[failure, content_type, accept, clientId, clientSecret, grantType, requestType, status, error_message, error_code, response_type] <<
[
["wrong grant_type",ContentType.URLENC,ContentType.JSON,ID,SECRET,"${GRANT}_wrong","POST", ApiUtility.ERRORS.'113'.status, ApiUtility.ERRORS.'113'.message, ApiUtility.ERRORS.'113'.code, ContentType.JSON.toString()],
["wrong Client_Secret",ContentType.URLENC,ContentType.JSON,ID,"${SECRET}_Wrong",GRANT,"POST", ApiUtility.ERRORS.'112'.status, ApiUtility.ERRORS.'112'.message, ApiUtility.ERRORS.'112'.code, ContentType.JSON.toString()],
["wrong Client_ID",ContentType.URLENC,ContentType.JSON,"${ID}_Wrong",SECRET,GRANT,"POST", ApiUtility.ERRORS.'112'.status, ApiUtility.ERRORS.'112'.message, ApiUtility.ERRORS.'112'.code, ContentType.JSON.toString()],
["mismatch of ID and Secret",ContentType.URLENC,ContentType.JSON,ID,otherToken,GRANT,"POST", ApiUtility.ERRORS.'112'.status, ApiUtility.ERRORS.'112'.message, ApiUtility.ERRORS.'112'.code, ContentType.JSON.toString()],
["an empty body",ContentType.URLENC,ContentType.JSON,"","","","POST", ApiUtility.ERRORS.'114'.status, "Missing or invalid parameters client_id,client_secret,grant_type", ApiUtility.ERRORS.'114'.code, ContentType.JSON.toString()],
["an empty grant_type",ContentType.URLENC,ContentType.JSON,ID,SECRET,"","POST", ApiUtility.ERRORS.'114'.status, "Missing or invalid parameters grant_type", ApiUtility.ERRORS.'114'.code, ContentType.JSON.toString()],
["an empty Client_Secret",ContentType.URLENC,ContentType.JSON,ID,"",GRANT,"POST", ApiUtility.ERRORS.'114'.status, "Missing or invalid parameters client_secret", ApiUtility.ERRORS.'114'.code, ContentType.JSON.toString()],
["an empty Client_ID",ContentType.URLENC,ContentType.JSON,"",SECRET,GRANT,"POST", ApiUtility.ERRORS.'114'.status, "Missing or invalid parameters client_id", ApiUtility.ERRORS.'114'.code, ContentType.JSON.toString()],
["wrong Content_Type",ContentType.JSON,ContentType.JSON,ID,SECRET,GRANT,"POST", ApiUtility.ERRORS.'114'.status, "Missing or invalid parameters client_id,client_secret,grant_type", ApiUtility.ERRORS.'114'.code, ContentType.JSON.toString()]
]
}