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()
、@Given
ステップで変更不可能なコレクションタイプをに割り当て、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を使用してDataTableを更新する標準的な方法を教えてもらえますか?
@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;
}
}
これを行うためのよりエレガントな方法がないことに驚いています。より良い提案があれば、私に知らせてください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加