黄瓜Java如何更新DataTable中的元素

时间:2019-02-04 23:01:50

标签: java cucumber cucumber-java

我正在尝试遵循Cucumber教程中的示例,但是它是为Ruby编写的,并且我试图以Java编写。我在执行@When步骤时遇到困难,因为它需要我更新DataTable并抛出以下异常,

java.lang.UnsupportedOperationException
at java.base/java.util.Collections$UnmodifiableList.set(Collections.java:1308)
at cucumber_tutorial.expressive_scenarios.chapter5.features.step_definitions.BoardSteps.player_x_plays_in_row_column(BoardSteps.java:36)
at ✽.When player x plays in row 2, column 1(tic_tac_toe.feature:8)

我的功能如下,

Feature:
  Scenario:
    Given a board like this:
      |   | 1 | 2 | 3 |
      | 1 |   |   |   |
      | 2 |   |   |   |
      | 3 |   |   |   |
    When player x plays in row 2, column 1
    Then the board should look like this:
      |   | 1 | 2 | 3 |
      | 1 |   |   |   |
      | 2 | x |   |   |
      | 3 |   |   |   |

运行功能并获取生成的代码段后,我有以下内容(我自己也添加了几行),

package cucumber_tutorial.expressive_scenarios.chapter5.features.step_definitions;

import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import java.util.List;

public class BoardSteps {

    List<List<String>> boardList;

    @Given("^a board like this:$")
    public void a_board_like_this(DataTable dataTable) throws Throwable {
        boardList = dataTable.raw();
        System.out.println(boardList);
        // 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("^player x plays in row (\\d+), column (\\d+)$")
    public void player_x_plays_in_row_column(int row, int col) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        //board.
        //throw new PendingException()
        boardList.get(row).set(col, "x");
    }

    @Then("^the board should look like this:$")
    public void the_board_should_look_like_this(DataTable expectedTable) 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)
        expectedTable.diff(boardList);
        //throw new PendingException();
    }

}

问题似乎是在dataTable.raw()步骤中的@GivenboardList分配了不可修改的集合类型,因此使其无法更新。 Ruby示例仅具有

row, col = row.to_i, col.to_i​   
@board[row][col] = ​'x'​

我的异常是从

引发的
boardList.get(row).set(col, "x"); //this line throws the exception

有人可以告诉我使用Java更新DataTable的标准方法是什么吗?

2 个答案:

答案 0 :(得分:0)

正如@Grasshopper在以上评论中建议的那样,我已经实现了转换功能 convertDataTableToModifiableList将不可修改的DataTable转换为我可以更新的List<List<String>>对象。我的工作解决方案如下所示,

import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import java.util.ArrayList;
import java.util.List;

public class BoardSteps {

    List<List<String>> boardList;

    @Given("^a board like this:$")
    public void a_board_like_this(DataTable dataTable) throws Throwable {
        boardList = convertDataTableToModifiableList(dataTable);
        System.out.println(boardList);
        // 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("^player x plays in row (\\d+), column (\\d+)$")
    public void player_x_plays_in_row_column(int row, int col) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        //board.
        //throw new PendingException()
        boardList.get(row).set(col, "x");
    }

    @Then("^the board should look like this:$")
    public void the_board_should_look_like_this(DataTable expectedTable) 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)
        expectedTable.diff(boardList);
        //throw new PendingException();
    }

    private List<List<String>> convertDataTableToModifiableList(DataTable dataTable){
        List<List<String>> lists = dataTable.asLists(String.class);
        List<List<String>> updateableLists = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++){
            List<String> list = lists.get(i);
            List<String> updateableList = new ArrayList<>();
            for (int j = 0; j < list.size(); j++){
                updateableList.add(j, list.get(j));
            }
            updateableLists.add(i, updateableList);
        }
        return updateableLists;
    }

}

我很惊讶没有更优雅的方法,如果您有更好的建议,请告诉我。

答案 1 :(得分:0)

这是我想出的解决方案。

public class BoardSteps {

    private ArrayList<List<String>> board;

    @Given("a board like this:")
    public void aBoardLikeThis(DataTable board) {
        this.board = new ArrayList<List<String>>();
        for (List<String> row: board.asLists()) {
            this.board.add(new ArrayList<String>(row));
        }

    }

    @When("player x plays in row {int}, column {int}")
    public void playerXPlaysInRowColumn(Integer row, Integer column) {
        this.board.get(row).set(column, "x");

    }

    @Then("the board should look like this:")
    public void theBoardShouldLookLikeThis(DataTable expectedBoard) {
        expectedBoard.diff(DataTable.create(board));
    }

}