Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flexible step names - @As annotation access to class fields #386

Closed
nnar1o opened this issue Feb 5, 2019 · 8 comments
Closed

flexible step names - @As annotation access to class fields #386

nnar1o opened this issue Feb 5, 2019 · 8 comments

Comments

@nnar1o
Copy link

nnar1o commented Feb 5, 2019

I have a scenario:

@Test
void test(){
  given().member_first_name();
  when().member_is_enrolled();
  then().first_name_is_valid();
}

class GivenMember {
  @ProvidedScenarioState
  String firstName;

  GivenMember member_first_name(){
    firstName = random();
  }
}

class ThenMember {
  @ExpectedScenarioState
  String firstName;

  @ExpectedScenarioState
  Member enrolledMember;

  GivenMember first_name_is_valid(){
    assertThat(enrolledMember.getFirstName()).isEqualTo(firstName);
  }
}

Is it possible to have in the report a dynamic step name that will include values from the stage (and not only method arguments)?
I.e.: something like this:

@ExpectedScenarioState
String firstName;

@As("first name of enrolled member is $firstName")
GivenMember first_name_is_valid()

Or somehow extend the AsProvider class to have access to ExpectedScenarioState fields? Maybe Other option can be adding a new method setStepName(String name) and call from method to adjust it's name in the report.

Now the workaround that I use in my code is like this ( this is ugly but works):

then().first_name_is_(given().getFirstName());

Best regards and thanks for help

@janschaefer
Copy link
Contributor

Interesting suggestion and also quite useful I think. There is already a possibility to inject the current step by using the @ScenarioState annotation together with the CurrentStep interface. See http://static.javadoc.io/com.tngtech.jgiven/jgiven-core/0.17.1/com/tngtech/jgiven/CurrentStep.html for details. I think this could be easily extended to be able to set the step name.

@janschaefer
Copy link
Contributor

Done. You can now do the following:

@ScenarioState
CurrentStep currentStep;

@ExpectedScenarioState
String firstName;

GivenMember first_name_is_valid() {
     currentStep.setName("first name of enrolled member is " + this.firstName);
}

@nnar1o
Copy link
Author

nnar1o commented Jul 25, 2019

When do you plan to release a new version?

@janschaefer
Copy link
Contributor

janschaefer commented Sep 1, 2019

Is released in version 0.18.0

@gandadil
Copy link

gandadil commented Sep 4, 2019

I can't see the Name property anywhere in HTML report, just on JSON report. Maybe HTML report is not reflecting the change?

@janschaefer
Copy link
Contributor

janschaefer commented Sep 6, 2019

Yes, indeed, you are right. But it is not the HTML report that is wrong, the JSON is actually wrong. The problem is that only the name of the step is changed, but not the words array.

@janschaefer janschaefer reopened this Sep 6, 2019
@janschaefer
Copy link
Contributor

I think I have not really thought this feature to the end. While updating the words array would be possible, the argument information would get lost. In parametrized scenarios this would mean that when the step name changes, JGiven will not be able to create data tables.

@janschaefer
Copy link
Contributor

I fixed this and updated the documentation to make clear that data tables cannot be created. Otherwise this should work now with the next 0.18.2 release.

janschaefer added a commit that referenced this issue Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants