gpt4 book ai didi

java - 从 cucumber 功能生成的步骤发送获取请求

转载 作者:行者123 更新时间:2023-12-02 13:48:15 26 4
gpt4 key购买 nike

我是 cucumber 的新手。我已经为 cucumber 配置了所需 jar 的环境。我想用cucumber测试rest api。因此,首先创建了 .feature 文件并生成了基本步骤定义。

.feature 文件:

Feature: Test

Scenario: List accounts
Given the system knows about the following details:
| name | value |
| unit | 01 |
| dept | 001 |
When the client requests accounts
Then the response code should be 200
And the response should contain following details:
| name | value |
| unit | 01 |
| dept | 001 |
| acctype | current |

TestClass.java如下:

package testPackage;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.DataTable;
import cucumber.api.PendingException;
import cucumber.api.java.en.*;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features={"classpath:cucumber.features/"},
glue = {"testPackage"}
)

public class TestClass {

@Given("^the system knows about the following details:$")
public void the_system_knows_about_the_following_details(DataTable arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
// For automatic transformation, change DataTable to one of
// List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
// E,K,V must be a scalar (String, Integer, Date, enum etc)
throw new PendingException();
}

@When("^the client requests accounts$")
public void the_client_requests_accounts() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

@Then("^the response code should be (\\d+)$")
public void the_response_code_should_be(int arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

@Then("^the response should contain following details:$")
public void the_response_should_contain_following_details(DataTable arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
// For automatic transformation, change DataTable to one of
// List<YourType>, List<List<E>>, List<Map<K,V>> or Map<K,V>.
// E,K,V must be a scalar (String, Integer, Date, enum etc)
throw new PendingException();
}

}

我搜索了很多,但找不到从 .java 文件发送 GET 请求。

如何从 cucumber 步骤定义发送 GET 请求并比较 json 响应?

最佳答案

如果你愿意使用Spring,可以使用Spring's REST Template以 RESTful 方式发送 HTTP 请求。

Jackson解析预定义 java 数据对象的响应/请求也非常好,这使得处理 JSON 的痛苦显着减轻。

关于java - 从 cucumber 功能生成的步骤发送获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32266062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com