[Cucumber] Expressions in Java

1,740 views
Skip to first unread message

Tim Walker

unread,
Jun 18, 2018, 2:22:56 PM6/18/18
to cu...@googlegroups.com

Hello,

 

I’m using cucumber expressions snd am struggling getting the basics working. I am not sure if I am doing it right as there was a little confusion around just providing the expression class like regular glue code (and it seems so simple!). Perhaps some can shed some light. 

 

With this Gherkin:

   Given a customer has a savings account

   Given a customer has a checking account

 

And this step:

   @Given("a customer has a {account_type} account")

   public void a_customer_has_a_account(AccountType account_type) {

       System.out.println("Has an account type: " + account_type);

   }

 

And this entry in config\cucumber.yml:

   typeRegistry.defineParameterType(new ParameterType<>(

     "account_type",       // name

     "savings|checking",    // regexp

    AccountType.class,    // type

     AccountType::new      // transformer function

     ))

 

With this inner-class in the steps file:

  public class AccountType {

               String accountType;

               AccountType(String accountType) {

                              this.accountType = accountType;

               }

  }

 

I get this:

Exception in thread "main" cucumber.runtime.CucumberException: Could not create a cucumber expression for 'a customer has a {account_type} account'.

It appears you did not register parameter type. The details are in the stacktrace below.

You can find the documentation here: https://docs.cucumber.io/cucumber/cucumber-

expressions/

        at io.cucumber.stepexpression.StepExpressionFactory.registerTypeInConfiguration(StepExpressionFactory.java:87)

        at io.cucumber.stepexpression.StepExpressionFactory.createExpression(StepExpressionFactory.java:65)

        at io.cucumber.stepexpression.StepExpressionFactory.createExpression(StepExpressionFactory.java:54)

 

--

Tim

Björn Rasmusson

unread,
Jun 18, 2018, 3:19:13 PM6/18/18
to Cukes
Halfordian Golfer wrote:

Hello,

 

I’m using cucumber expressions snd am struggling getting the basics working. I am not sure if I am doing it right as there was a little confusion around just providing the expression class like regular glue code (and it seems so simple!). Perhaps some can shed some light. 

 

With this Gherkin:

   Given a customer has a savings account

   Given a customer has a checking account

 

And this step:

   @Given("a customer has a {account_type} account")

   public void a_customer_has_a_account(AccountType account_type) {

       System.out.println("Has an account type: " + account_type);

   }

 

And this entry in config\cucumber.yml:


Cucumber-JVM does not use cucumber.yml. The java-calculator example uses ParameterTypes.java to define parameter types.

/Björn

Tim Walker

unread,
Jun 18, 2018, 4:00:06 PM6/18/18
to cu...@googlegroups.com
Björn thank you. [tim] below inline...

On Mon, Jun 18, 2018, 12:19 Björn Rasmusson <bj.ras...@gmail.com> wrote:
Halfordian Golfer wrote:

Hello,

 

I’m using cucumber expressions snd am struggling getting the basics working. I am not sure if I am doing it right as there was a little confusion around just providing the expression class like regular glue code (and it seems so simple!). Perhaps some can shed some light. 

 

With this Gherkin:

   Given a customer has a savings account

   Given a customer has a checking account

 

And this step:

   @Given("a customer has a {account_type} account")

   public void a_customer_has_a_account(AccountType account_type) {

       System.out.println("Has an account type: " + account_type);

   }

 

And this entry in config\cucumber.yml:


Cucumber-JVM does not use cucumber.yml. The java-calculator example uses ParameterTypes.java to define parameter types.

[Tim] Thank you very much. This is not at all obvious as the stack trace "Register Type In Configuration" - sure looks like it wants to give it a go from the YML! So, to be clear, don;t register parameter types using profiles or configurations but, rather, leave an "extended ParameterType.java" in the path to do the deed? Thanks again. Tim
 
/Björn

 

   typeRegistry.defineParameterType(new ParameterType<>(

     "account_type",       // name

     "savings|checking",    // regexp

    AccountType.class,    // type

     AccountType::new      // transformer function

     ))

 

With this inner-class in the steps file:

  public class AccountType {

               String accountType;

               AccountType(String accountType) {

                              this.accountType = accountType;

               }

  }

 

I get this:

Exception in thread "main" cucumber.runtime.CucumberException: Could not create a cucumber expression for 'a customer has a {account_type} account'.

It appears you did not register parameter type. The details are in the stacktrace below.

You can find the documentation here: https://docs.cucumber.io/cucumber/cucumber-

expressions/

        at io.cucumber.stepexpression.StepExpressionFactory.registerTypeInConfiguration(StepExpressionFactory.java:87)

        at io.cucumber.stepexpression.StepExpressionFactory.createExpression(StepExpressionFactory.java:65)

        at io.cucumber.stepexpression.StepExpressionFactory.createExpression(StepExpressionFactory.java:54)

 

--

Tim

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages