cucumber-java-skeleton icon indicating copy to clipboard operation
cucumber-java-skeleton copied to clipboard

StepDefinitions should have at least one assert to call it as BDD test

Open nagkumar opened this issue 1 year ago • 2 comments

https://github.com/cucumber/cucumber-java-skeleton/blob/main/gradle/src/test/java/io/cucumber/skeleton/StepDefinitions.java

any test should have assert to make it clear what it is testing

image

nagkumar avatar Apr 17 '23 11:04 nagkumar

I agree that "Then" step definitions should have at least one assert. But asserts should be optional for "Given" and "When" step definitions:

  • "Given" step definitions defines the testing context : you could use assert to ensure your testing context is coherent (e.g. no negative number of cukes)
  • "Then" step definitions define some action to do on the testing context : you could use assert to ensure your testing context is coherent (e.g. number of cukes has been defined)

So the @Given "I have {int} cukes in my belly" example should be replaced with a @Then step definition to make the issue more coherent.

By the way, it is true that this @Given example is not very good as it creates a Belly but this object instance is automatically deleted by the JVM when the method ends, so it cannot be used in another step definition (the line 8 Belly belly = new Belly(); should be defined as a field to do so).

jkronegg avatar Apr 17 '23 19:04 jkronegg

You are looking at an intentionally incomplete project. The When and Then steps are present in the scenario:

https://github.com/cucumber/cucumber-java-skeleton/blob/d262b8dc017d01db5425c9b3727e411e36c32498/maven/src/test/resources/io/cucumber/skeleton/belly.feature#L1-L6

And this example project was written using a test driven approach. So just enough code has been written to get the tests running and failing. When run Cucumber will complain about two missing step definitions. You can add those and then incrementally implement the scenario.

This is great for people who are learning TDD, but probably less useful as a minimal working example. Please do feel free to workshop a better but still minimal example.

mpkorstanje avatar Apr 17 '23 20:04 mpkorstanje