cucumber-jvm 3.x.x中,我想将只有两列的数据表转换为单个对象。过去它在2.x.x中是自动的,但后来在新的主要版本中已删除。

代码:

// Feature
Scenario: Scan Barcode
    Given I Am At The Login Page
    When I Log In As Valid User
        | group    | gpao     |
        | username | svgpao1  |
        | password | REDACTED |

// Stepdef
@When("I Log In As Valid User")
public void I_Log_In_As_Valid_User(User user) throws Throwable {
    Selenium.enterText(driver, user.getUsername(), "username");
    Selenium.enterText(driver, user.getPassword(), "password");
    Selenium.clickElement(driver, "sign.in");
    LOGGER.debug("User Group=" + user.getGroup());
}

// Configurer
registry.defineDataTableType(new DataTableType(User.class, new TableTransformer<User>() {
    @Override
    public User transform(DataTable dataTable) throws Throwable {
        Map<String, String> map = dataTable.asMap(String.class, String.class);
        return new User(map);
    }
}));

// User - constructor
public User(Map<String, String> map) {
    this.group = map.get("group");
    this.username = map.get("username");
    this.password = map.get("password");
}

堆栈跟踪:
cucumber.runtime.CucumberException: Could not convert arguments for step [I Log In As Valid User] defined at 'jcucumberng.project.stepdefs.EnterPacketDetailsSteps.I_Log_In_As_Valid_User(User) in file:/Z:/workspace/java/jcucumberng-framework/target/test-classes/'.
The details are in the stacktrace below.
        at cucumber.runtime.PickleStepDefinitionMatch.couldNotConvertArguments(PickleStepDefinitionMatch.java:69)
        at cucumber.runtime.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:46)
        at cucumber.runner.TestStep.executeStep(TestStep.java:55)
        at cucumber.runner.TestStep.run(TestStep.java:42)
        at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:53)
        at cucumber.runner.TestCase.run(TestCase.java:47)
        at cucumber.runner.Runner.runPickle(Runner.java:44)
        at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:140)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:98)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:55)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at cucumber.api.junit.Cucumber$1.evaluate(Cucumber.java:107)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417)
Caused by: io.cucumber.datatable.CucumberDataTableException: 'jcucumberng.project.domain.User' could not transform
      | group    | gpao     |
      | username | svgpao1  |
      | password | REDACTED |

最佳答案

除了@Transpose之外,还需要修改transform方法。

public User transform(DataTable dataTable) throws Throwable {
    return new User(dataTable.asMaps().get(0));
}

@When("I Log In As Valid User")
public void I_Log_In_As_Valid_User(@Transpose SilverUser user) throws Throwable {
    System.out.println(user);
}

10-06 05:46