Search code examples
javaserenity-bddassertj

Serenity BDD : How to loop on Steps with Soft Assertions


I need to run tests on an array of datas, and I can't find a way to do a soft assert in my step AND show the error at the correct step in the Serenity Report.

Example code

@Then("All my datas are correct")
public void verifyMyDatas(){

    int[] myDataArray = new int[] {1,2,3,4};

    for(int i = 0; i < myDataArray.length; i++){

        mySteps.myAwesomeValidator(myDataArray[i]);
    }
}

And an example step :

@Step("Checking the value {0}")
public void myAwesomeValidator(int value){

    //I need a soft assertion here
}

My attempt :

I tried using the assertj framework. But my problem with it is that the "All my datas are correct" step is correctly flagged as a FAILURE, but all the substeps "Checking the value X" are marked as SUCCESSes on Serenity's report.

my test code :

@Then("All my datas are correct")
public void verifyMyDatas(){

    SoftAssertions softAssertion = new SoftAssertions();

    int[] myDataArray = new int[] {1,2,3,4};

    for(int i = 0; i < myDataArray.length; i++){

my mySteps.myAwesomeValidator(myDataArray[i], softAssertion); }

    softAssertion.assertAll();
}

And the step :

@Step("Checking the value {0}")
public void myAwesomeValidator(int value, SoftAssertions softAssertion){

    softAssertion.assertThat(value < 3).isTrue();
}

Edit : tried to clarify the problem with my attempt


Solution

  • I would try as() to describe the assertion and not introduce a Step to see if it works (I believe it should):

    @Then("All my datas are correct")
    public void verifyMyDatas(){
    
      SoftAssertions softAssertion = new SoftAssertions();
    
      int[] myDataArray = new int[] {1,2,3,4};
      for(int i = 0; i < myDataArray.length; i++) {
        myAwesomeValidator(myDataArray[i], softAssertion); 
      }
    
      softAssertion.assertAll();
    }
    
    public void myAwesomeValidator(int value, SoftAssertions softAssertion){
    
      // use as() to describe the assertion 
      softAssertion.assertThat(value)
                   .as("awesomely validate value %d", value);
                   .isLessThan(3);
    }