如何更新数据表中的元素
我试图遵循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();
}
}
问题似乎是@Given
步骤中的dataTable.raw()
为boardList
分配了一个不可修改的集合类型,因此使其不可更新。
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更新数据表的标准方法是什么吗
共 (0) 个答案