Hi Russell,

on context and state JBehave's approach is simplicity and power to the
users, i.e. it's only concerned with parsing and executing (in a single
thread) the story with the scenarios and steps in the order in which
they are found, regardless of which Steps class they are in, and letting
the Steps implementers manage state and context.

So, yes, if you execute a step that requires an instance (such as the
Stock below) without first having initialised the instance, it will
throw a NPE.

If you do want to keep state, it's up to you how to best achieve that.  
To share context objects you may inject a singleton into more than one
Steps class.

As for "best practices", it depends on the testing context.  Generally,
if you keep state via member variables it's best to maintain all steps
that access these instance variables in the same Steps class.  But you
may well interface to others systems,  such as databases or queues,  in
which case it matters less where how the steps methods are organised. 
As always, readability and simplicity of the Steps code is important
(testing code is code and should be maintained like production code).

Cheers

On 17/02/2011 15:44, Russell Brown wrote:
> Hi,
> I'm trying to get my head around JBehave right now. I love using it and it is 
> a great BDD project. My project is getting pretty big now and there are quite 
> a lot of steps. We are striving for step reuse but I have some questions 
> about the context and order of step execution.
>
> If I use the example from your documents 
> http://jbehave.org/reference/stable/developing-stories.html
>
> In this case, if the
>
>     When the stock is traded at 5.0
>
>     @When("the stock is traded at $price")
>     public void theStockIsTradedAt(double price) {
>         stock.tradeAt(price);
>     }
>
> step  is used by a different story, one that didn't start with the
>
>
>     Given a stock of symbol STK1 and a threshold of 10.0
>
>      @Given("a stock of symbol $symbol and a threshold of $threshold")
>     public void aStock(String symbol, double threshold) {
>         stock = new Stock(symbol, threshold);
>     }
>
>
> Then you have an NPE? Is that right?
>
> My question is really about how to manage state (or context) for a Step 
> class. If your steps create/modify/read state from the enclosing class then 
> they cannot really be mixed and matched, or can they?
> Also, thread wise... I assume that you guarantee that only one thread runs 
> the steps in sequence? And that only a single story is run at a time (IE the 
> app is totally single threaded in its execution)?
>
> In the case that a Given step from one class proceeds a When step from 
> another class which proceeds a Then step from a third class, is there any 
> shared memory that they call access (like a Story Context or something?)
>
> In the case that Spring is used, all the steps will be singletons...with 
> state...and resused steps may read stale state...so many questions.
>
> Is there a simple, best practice style answer like "All Steps should be in 
> the same class" or "Stories don't have state" or something like that?
>
> Love JBehave but I really need  a steer on the above: State, Order, Threads
>
> Many thanks
>
> Russell
>
> This e-mail and any attachments are confidential and intended solely for the 
> addressee and may also be privileged or exempt from disclosure under 
> applicable law. If you are not the addressee, or have received this e-mail in 
> error, please notify the sender immediately, delete it from your system and 
> do not copy, disclose or otherwise act upon any part of this e-mail or its 
> attachments.
>
> Internet communications are not guaranteed to be secure or virus-free. 
> ioko365 Limited does not accept responsibility for any loss arising from 
> unauthorised access to, or interference with, any Internet communications by 
> any third party, or from the transmission of any viruses.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to