在IntelliJ中运行功能文件时缺少步骤

时间:2019-04-26 15:53:01

标签: java intellij-idea cucumber bdd

Intellij在运行我的功能文件时一直说“未定义的步骤”。但是,我已经复制了步骤并将其放入另一个程序包中,并将该程序包名称添加到“ Edit”配置的“ glue”参数中。仍然无法正常工作。

我已经添加了功能文件,它没有显示任何缺少的步骤参考。我正在添加屏幕截图。我已经在另一个软件包中添加了所有步骤。 请看下面

enter image description here

国家步骤代码如下

package Steps;

import Fixtures.Countries;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;

public class CountriesSteps {

Countries countriesclass = new Countries();

@Given("^I generate a restful request for countries$")
public void iGenerateARestfulRequestForCountries() throws Throwable {
    countriesclass.GetCountries();
}

@When("^I receive a successful country response (.*)$")
public void iReceiveASuccessfulCountryResponseResponse(int code) throws Throwable {
    Assert.assertEquals(code, countriesclass.getsCode());
}

@When("^I receive a successful country response$")
public void iReceiveASuccessfulCountryResponse() throws Throwable {         
    Assert.assertEquals(200, countriesclass.getsCode());
}

@Then("^the api country response returns (.*)$")
public void theApiCountryResponseReturnsCountries(int countries) throws Throwable {         
    Assert.assertEquals(countries, countriesclass.getCount());
}

@Then("^the api country response returns (.*),(.*),(.*),(.*),(.*)$")
public void theApiCountryResponseReturnsCountriesNameCapitalPopulation(int countries, int index, String name, String capital, int population) throws Throwable {         
    Assert.assertEquals(countries, countriesclass.getCount());
}

@Then("^the api country response for Index (.*) returns (.*),(.*),(.*)$")
public void theApiCountryResponseForIndexIndexReturnsNameCapitalPopulation(int index, String name, String capital, int population) throws Throwable {
    //Validate a few values from response        
    Assert.assertEquals(name, countriesclass.getcList().get(index).name);
    Assert.assertEquals(capital, countriesclass.getcList().get(index).capital);
    Assert.assertEquals(population, countriesclass.getcList().get(index).population);
}
}

国家代码是

package Fixtures;

import Models.CountriesData;
import com.jayway.restassured.response.Response;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.reflect.TypeToken;
import org.json.JSONArray;

import java.lang.reflect.Type; 
import java.util.ArrayList;
import java.util.List;

import static com.jayway.restassured.RestAssured.get;

public class Countries {

private static String url;
private static int count;
private static int sCode;
private static List<CountriesData> cList;


public void GetCountries() throws Exception
{
    try        {
        url = "http://restcountries.eu/rest/v1/all";

        // make get request to fetch json response from restcountries
        Response resp = get(url);

        //Fetching response in JSON as a string then convert to JSON Array
        JSONArray jsonResponse = new JSONArray(resp.asString());

        count = jsonResponse.length(); // how many items in the array
        sCode = resp.statusCode();  // status code of 200

        //create new arraylist to match CountriesData
        List<CountriesData> cDataList = new ArrayList<CountriesData>();

        Gson gson = new Gson();
        Type listType = new TypeToken<List<CountriesData>>() {}.getType();

        cDataList = gson.fromJson(jsonResponse.toString(), listType);

        cList = cDataList;

    }
    catch (Exception e)
    {
        System.out.println("There is an error connecting to the API: " + e);
        e.getStackTrace();
    }
}

//getters to return ('get) the values
public int getsCode() {
    return sCode;
}

public int getCount() {
    return count;
}

public List<CountriesData> getcList()    {
    return cList;
}

}

1 个答案:

答案 0 :(得分:1)

在创建新的步骤定义文件时,默认情况下,IntelliJ建议文件路径为\IdeaProjects\RestAPI\src\main\resources\stepmethods

您的步骤定义文件夹为\IdeaProjects\RestAPI\src\test\java\Steps。确保黄瓜没有放在错误的位置。