问题
In cucumber-jvm 3.x.x
, I want to convert a datatable with only 2 columns to a single object. This used to be automatic in 2.x.x
but has since been removed with the new major version.
Code:
// 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");
}
Stacktrace:
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 |
回答1:
You need to also modify the transform method in addition to @Transpose
.
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);
}
来源:https://stackoverflow.com/questions/51839707/cucumber-jvm-3-convert-datatable-to-single-object-using-asmap