gpt4 book ai didi

Cucumber StepDefinition (JAVA) 中的 java.lang.Error

转载 作者:行者123 更新时间:2023-12-01 09:50:08 24 4
gpt4 key购买 nike

刚接触 Java,但对 Cucumber 相当了解。

这是我的功能步骤:@api_test功能:您的功能的标题 我想将此模板用于我的功能文件

场景:显示博客文章

假设我访问资源网址“/comments?id=2”

当我检索结果时

那么状态码应该是200

并且它应该具有包含值“2”的“id”字段

并且它应该具有包含值“Jayne_Kuhic@sydney.com”的“电子邮件”字段

<小时/>

运行功能测试时,出现以下错误:

java.lang.Error: Unresolved compilation problems: 
wc cannot be resolved
httpStatus cannot be resolved or is not a field
HttpStatusCode cannot be resolved to a variable
WebException cannot be resolved to a type
httpStatus cannot be resolved or is not a field
HttpWebResponse cannot be resolved to a type
HttpWebResponse cannot be resolved to a type
httpStatus cannot be resolved or is not a field
HttpStatusCode cannot be resolved to a variable
Assert cannot be resolved
response cannot be resolved or is not a field
JObject cannot be resolved to a type
JToken cannot be resolved

这是我的代码:

package stepDefinition;

import java.io.*;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import org.json.*;
import org.junit.Assert.*;

public class RestTestSteps {

private String url;
private String content;
private WebClient wc = new WebClient();
JObject response;
HttpStatusCode httpStatus;



@Given("^I access the resource url \"([^\"]*)\"$")
public void i_access_the_resource_url(String resourceUrl) throws Throwable {
// Write code here that turns the phrase above into concrete actions
this.url = "http://jsonplaceholder.typicode.com" + resourceUrl;
}

@When("^I retrieve the results$")
public void i_retrieve_the_results() throws Throwable {
// Write code here that turns the phrase above into concrete actions
try
{
this.content = wc.DownloadString(url);
this.httpStatus = HttpStatusCode.OK;
}
catch (WebException we)
{
this.httpStatus = ((HttpWebResponse)we.Response).StatusCode;
}
if (this.httpStatus.Equals(HttpStatusCode.OK))
{
Assert.IsNotNullOrEmpty(this.content);
this.response = (JObject)JToken.Parse(this.content);
}

}

@Then("^The status code should be (\\d+)$")
public void the_status_code_should_be(int statusCode) throws Throwable {
// Write code here that turns the phrase above into concrete actions

Assert.AreEqual(statusCode, (int)this.httpStatus);

}

@Then("^It should have the field \"([^\"]*)\" containing the value \"([^\"]*)\"$")
public void it_should_have_the_field_containing_the_value(String arg1, String arg2) throws Throwable {
// Write code here that turns the phrase above into concrete actions

if (response != null)
{
JValue val = (JValue)this.response.GetValue(field);
string valStr = val.Value().Trim();
Assert.IsNotNull(valStr);
Assert.AreEqual(valStr, value.Trim());
}

}

}

不确定我在这里做错了什么......

最佳答案

在我看来,您缺少进口。我看到了 cucumber 内容的导入,但我认为您需要导入您正在使用的其他类。如果您使用的是 eclipse 这样的 IDE,它应该可以帮助您添加必要的导入。

此外,您没有正确导入断言。这些需要作为静态导入。


导入静态 org.junit.Assert.*;

关于Cucumber StepDefinition (JAVA) 中的 java.lang.Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648280/

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