Cucumber JavaDataTableの要素を更新する方法

robbie70

Cucumberチュートリアルの例に従おうとしていますが、Ruby用に書かれており、Javaで書き込もうとしています。@WhenDataTableを更新する必要があり、次の例外がスローされるため、この手順を実装するのに問題があります。

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を更新する標準的な方法を教えてもらえますか?

robbie70

@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]

編集
0

コメントを追加

0

関連記事

分類Dev

Cucumber + TestNGとCucumber + JUNITの比較

分類Dev

Cucumber-JVMの--formatオプションを更新する方法は?

分類Dev

TestFXとCucumberの使用

分類Dev

SpringをCucumberに統合する方法

分類Dev

SonarqubeとCucumberの機能

分類Dev

cucumber-junit-platform-engineの@CucumberOptions

分類Dev

Cucumber Java:JSONプラグインの生成を強制する方法

分類Dev

5.0.0以降のio.cucumber.junit.Cucumber.javaでgetDescription()をオーバーライドする方法はありますか?

分類Dev

'mvntest'を使用してCucumberテストを実行する方法

分類Dev

Cucumber DataTableエラー-io.cucumber.datatable.UndefinedDataTableTypeException:DataTableをcucumber.api.DataTableに変換できません

分類Dev

IDEなしでSlenium + Java + Cucumberを実行する方法

分類Dev

HashMapをcucumber dataTableに変換する方法は?

分類Dev

Visual Studio Code(VSCode)でCucumberをデバッグする方法は?

分類Dev

Cucumber ruby ParameterTypeを使用して特定の式を変換する方法は?

分類Dev

CucumberでのJava 8時間クラス

分類Dev

Selenium Cucumber Repository(Java)Picocontainerの問題

分類Dev

Cucumberの論理ANDおよびORタグ

分類Dev

Cucumber-JVMMaven統合の問題

分類Dev

要素を見つけて、後でそれを再利用するcapybara cucumber

分類Dev

AndroidStudioでCucumber機能ファイルのサポートを追加する方法

分類Dev

Gradle 5とCucumber-JVMを使用して別のディレクトリからCucumberを実行する

分類Dev

Cucumberテストがcucumber.propertiesファイルを読み取らない

分類Dev

cucumber-protactor-typescriptcssによってスリープと要素を設定する正しい方法

分類Dev

Cucumberでデータのテーブルを変換する

分類Dev

CucumberのAssertJ Swingテストを使用する

分類Dev

Cucumber Extent ReportsをCucumber 5.6.0で使用するにはどうすればよいですか?

分類Dev

cucumber.adapter.ExtentCucumberAdapterを統合した後、初期化エラー「NoClassDefFoundError:cucumber / runtime / io / URLOutputStream」を取得する

分類Dev

cucumber-jvmステップ間で変数を渡す方法

分類Dev

TestNGパラメータをCucumberに渡す方法は?

Related 関連記事

  1. 1

    Cucumber + TestNGとCucumber + JUNITの比較

  2. 2

    Cucumber-JVMの--formatオプションを更新する方法は?

  3. 3

    TestFXとCucumberの使用

  4. 4

    SpringをCucumberに統合する方法

  5. 5

    SonarqubeとCucumberの機能

  6. 6

    cucumber-junit-platform-engineの@CucumberOptions

  7. 7

    Cucumber Java:JSONプラグインの生成を強制する方法

  8. 8

    5.0.0以降のio.cucumber.junit.Cucumber.javaでgetDescription()をオーバーライドする方法はありますか?

  9. 9

    'mvntest'を使用してCucumberテストを実行する方法

  10. 10

    Cucumber DataTableエラー-io.cucumber.datatable.UndefinedDataTableTypeException:DataTableをcucumber.api.DataTableに変換できません

  11. 11

    IDEなしでSlenium + Java + Cucumberを実行する方法

  12. 12

    HashMapをcucumber dataTableに変換する方法は?

  13. 13

    Visual Studio Code(VSCode)でCucumberをデバッグする方法は?

  14. 14

    Cucumber ruby ParameterTypeを使用して特定の式を変換する方法は?

  15. 15

    CucumberでのJava 8時間クラス

  16. 16

    Selenium Cucumber Repository(Java)Picocontainerの問題

  17. 17

    Cucumberの論理ANDおよびORタグ

  18. 18

    Cucumber-JVMMaven統合の問題

  19. 19

    要素を見つけて、後でそれを再利用するcapybara cucumber

  20. 20

    AndroidStudioでCucumber機能ファイルのサポートを追加する方法

  21. 21

    Gradle 5とCucumber-JVMを使用して別のディレクトリからCucumberを実行する

  22. 22

    Cucumberテストがcucumber.propertiesファイルを読み取らない

  23. 23

    cucumber-protactor-typescriptcssによってスリープと要素を設定する正しい方法

  24. 24

    Cucumberでデータのテーブルを変換する

  25. 25

    CucumberのAssertJ Swingテストを使用する

  26. 26

    Cucumber Extent ReportsをCucumber 5.6.0で使用するにはどうすればよいですか?

  27. 27

    cucumber.adapter.ExtentCucumberAdapterを統合した後、初期化エラー「NoClassDefFoundError:cucumber / runtime / io / URLOutputStream」を取得する

  28. 28

    cucumber-jvmステップ間で変数を渡す方法

  29. 29

    TestNGパラメータをCucumberに渡す方法は?

ホットタグ

アーカイブ