Search code examples
javajbehave

Why Composite steps in jBehave doesn't execute pre-conditions first?


I am automating my web application with JBehave where I face following issue.

I have created one composite steps which includes all the pre-conditions.. When I run the steps, it first executes what is inside the composite step rather than running pre-conditions first..

How can I run the tests sequentially by using composite steps. Please advise.

Following is my steps file code.

@Given("I see given step")
    public void given()
    {
        System.out.println("Inside GIVEN");
    }

    @Then("I see then step")
    public void when()
    {
        System.out.println("Inside WHEN");
    }

    @Then("I see when step")
    public void then()
    {
        System.out.println("Inside THEN");
    }

    @Given("I see composite step")
    @Composite(steps={"Given I see given step","Then I see then step","When I see when step"})
    public void composite()
    {
        System.out.println("Inside COMPOSITE");
    }

When I run "Given I see composite step", the sysout which is inside composite function runs first.. I need to execute the pre-conditions first.

Thanks!


Solution

  • The steps inside the @Composite are not meant as preconditions for the implementation of your @Composite-Annotated-Method...

    If you are happy with Scenario/Story based pre-conditions you could consider the JBehave Lifecycle.

    If you need pre-conditions at step bases (as you said) I would fake it, by adding a 4th JBehave step, that is called last in your @Composite block.

    Given your example:

        @Given("I see given step")
        public void given()
        {
            System.out.println("Inside GIVEN");
        }
    
        @When("I see when step")
        public void then()
        {
            System.out.println("Inside THEN");
        }
    
        @Then("I see then step")
        public void when()
        {
            System.out.println("Inside WHEN");
        }
    
        @Then("your step executed last")
        public void then()
        {
            // logic from composite()
            System.out.println("Inside COMPOSITE");
        }
    
        @Given("I see composite step")
        @Composite(steps={"Given I see given step","When I see when step","Then I see then step","Then your step executed last"})
        public void composite()
        {
             //logic got moved
        }