黄瓜StepDefinition(JAVA)中的java.lang.Error

时间:2016-06-06 01:00:11

标签: java eclipse cucumber cucumber-jvm

Java新手,但对Cucumber非常了解。

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

场景:显示博客帖子

鉴于我访问了资源网址“/ comments?id = 2”

检索结果时

然后状态代码应为200

它的字段“id”应包含值“2”

它应该有包含值“Jayne_Kuhic@sydney.com”的字段“email”

运行功能测试时,我收到以下错误:

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());
		    }
		
	}

}

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

1 个答案:

答案 0 :(得分:1)

在我看来你缺少进口。我看到黄瓜的进口,但我认为你需要导入你正在使用的其他类。如果你在像eclipse这样的IDE中,它应该可以帮助你添加必要的导入。

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

import static org.junit.Assert.*;