Cucumber 内联数据 table 转换和迭代

Cucumber in-line data table conversion and iteration

一个相当基本的问题,因为我在长期中断后习惯了 Cucumber,并且也跟上了 Java 8 和 lambda 的速度。我想知道是否有人可以指出 better/optimal 执行此简单步骤定义的方法:

给定黄瓜场景:

Scenario:Student submits grades 
    Given a student has completed a paper
    When they have submitted the following grades 
      | math     | good |
      | physics  | poor |
    Then the grades website shows the correct grading

以及需要按以下格式发送到 api 的数据:

iteration 1:
math:good
iteration 2:
physics:poor

...在 Then 中断言两个报告都按预期显示之前。

以及麻烦的步骤定义:

 @When("they have submitted the following grades")
    public void theyHaveSubmittedTheFollowingGrades(Map<String, String> reportData) {

        String reportData;

        reportData.forEach((subject, grade) ->

        report = String.join(subject, ":", grade));

        sendReportGrades(report);

        ); //Unexpected token error here in IntelliJ
    }

so sendReportGrades(report) 方法仅在 subject:grade 格式时调用才会应用成绩的服务,并且它需要发送两个报告(或者 table 中有很多报告)。

怎么样

public void theyHaveSubmittedTheFollowingGrades(Map<String, String> reportData) {
    reportData.forEach((subject, grade) -> sendReportGrades(String.join(subject, ":", grade)));
}

我会使用 POJO 和依赖注入来使其更具可读性。 使用 Pico 容器或 Spring 进行依赖注入,如下所述:https://cucumber.io/docs/cucumber/state/

In the Feature file (using a header in the data table of the When step) :
When they have submitted the following grades
  | subject | grade |
  | math    | good  |
  | physics | poor  |

POJO

public class ReportData {

  private String subject;
 
  private String grade;

  public ReportData(String subject, String grade) {
    this.subject = subject;
    this.grade = grade;
  }

  @Override
  public String toString() {
    return String.format("%s:%s", subject, grade);
  }
}

步骤定义

@When("they have submitted the following grades")
public void theyHaveSubmittedTheFollowingGrades(List<ReportData> reportData) {
    reportData.stream()
        .map(ReportData::toString)
        .forEach(r -> sendReportGrades(r));
}

private void sendReportGrades(String report) {
    // Calling system under test
    ...
    System.out.println("report sent -> " + report);
}

在 Cucumber 3.x 之前,从数据 table 到 POJO 的依赖注入使用 xstream 所以它真的是即插即用,但是自从 3.x ,您必须在步骤定义 class :

中像这样声明从 Data Table 到 ReportData POJO class 的映射
@DataTableType
public ReportData convert(Map<String, String> entry){
  return new ReportData(
     entry.get("subject"),
     entry.get("grade")
  );
}

它给你:

report sent -> math:good
report sent -> physics:poor