1
Given a stock threshold of 10.0
When stock threshold is set to 10.0
Then threshold result should be 10.0

In above steps, instead of the constant value 10.0, I'd like to use a predefined variable. Like,

Given a stock threshold of <thresholdValue>
When stock threshold is set to <thresholdValue>
Then threshold result should be <thresholdValue>

This predefined variable can be a global variable for all stories. And the value can be initialized in step classes, or story file itself. Can this kind of implementation can be done in JBehave.

Aki T
  • 612
  • 2
  • 7
  • 17
  • don't just drop an assignment here, we are not going to do your homework for you. Also interesting to know: Java doesn't support global variables. – Stultuske Mar 11 '20 at 09:26
  • @Stultuske I think you didn't get the question here. I said "a global variable for all stories'. Which means a common variable for all story files in JBehave. And this is a legit question, which is not explained in JBehave documentation – Aki T Mar 11 '20 at 09:33
  • "this is a legit question", so far, you haven't tried anything. JBehave is a Java framework. Java doesn't support global variables, but still. I assume you have heard of static variables? What is stopping you from creating a class with those values as static variables? – Stultuske Mar 11 '20 at 09:38
  • @Stultuske I have no issue with Java static variables. This issue is totally related to JBehave story files. If you've read the topic "Define a reusable variable and use it inside story files in JBehave". I clearly mentioned define something and use it inside a JBehave story file. Not inside a Java file. – Aki T Mar 11 '20 at 10:08
  • ah, so you tag this as Java because it isn't a Java issue. Smart move. – Stultuske Mar 11 '20 at 10:19
  • @Stultuske I'm using JBehave with Java. Sorry If you got confused. – Aki T Mar 11 '20 at 10:29
  • have you tried, instead of something like this: @Given("a stock of symbol $symbol and a threshold of $threshold") public void aStock(String symbol, double threshold) { stock = new Stock(symbol, threshold); }, something like this: @Given("a stock of symbol $symbol and a threshold of $threshold") public void aStock(String symbol, double threshold) { stock = new Stock(symbol, MyStaticVariables.staticThreshold); } ? – Stultuske Mar 11 '20 at 10:34

1 Answers1

0

If the threshold value is the same in each story then I wouldn't hard code it into the steps. If there is significance to the value 10.0 then I would give it a name that makes sense to the business:

Given an average stock threshold
Given a nominal stock threshold

Or just forgo the value all together and simplify each step:

Given a stock threshold
When the stock threshold is set
Then the stock threshold should be met

If having a hard coded value for each step is absolutely necessary, either copy and paste the value and just live with it, or use different values in each scenario. Often times a variance in test data is better than using the same test data over and over.

Basically if 10.0 does not have any business significance, omit the value and let the value be hard coded in the step definition, or copy and paste the value. Don't waste a lot of thought on this.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • What if that value is used in 10,000 steps, and suddenly that value needs to be changed. I'll have to go and update all those 10,000 steps right? If that value is defined somewhere as a variable, I'll just go and update that value. – Aki T Mar 11 '20 at 11:20
  • @AkiT: why does the value need to change? If it needs to change I would not hard code it for sure, but the real question is *does it need to change?* If it does then I would omit the value from the feature files and hard code it in the step definitions. – Greg Burghardt Mar 11 '20 at 12:07
  • Unfortunately It can be changed in the future. – Aki T Mar 11 '20 at 12:09
  • What is the significance of `10.0` then? – Greg Burghardt Mar 11 '20 at 12:59
  • Just an example to give the idea. – Aki T Mar 11 '20 at 13:00
  • I don't think we have enough information to answer the question, then. You might be creating a lot of work for yourself without proper justification. I would just omit that number from the steps and hard code that number in the step definitions, in that case. – Greg Burghardt Mar 11 '20 at 13:12