anchor.diff: -== Stages and State Injection
  +== Stages and State Sharing
   :sourcedir: ../jgiven-examples/src/test/java/com/tngtech/jgiven/examples
   
   
  
positive.code: }
  ----
  
  === State Injection
  
  Stages share state by using injection. This works by annotating the fields with a special annotation `@ScenarioState`. The values of these fields are shared between all stages that have the same field.
  
  For example, to be able to access the value of the ingredients field of the GivenIngredients stage in the `WhenCook` stage one has to annotate that field accordingly:
  
  
  
  [source,java]
  ----
  include::{sourcedir}/pancakes/test/steps/GivenIngredients.java[]
  
negative.code: include::{sourcedir}/pancakes/test/steps/WhenCook.java[tags=state]
  ----
  
  Instead of the `@ScenarioState` annotation one can also use `@ExpectedScenarioState` and `@ProvidedScenarioState` to indicate whether the state is expected by the stage or provided by the stage.
  These function in exactly the same way as `@ScenarioState` but are more descriptive about what the code is doing.
  
  ==== Type vs. Name Resolution
  
  Scenario state fields are by default resolved by its type.
  That is, you can only have one field of the same type as a scenario field.
  Exceptions are types from the packages `java.lang.\*` and `java.util.*` which are resolved by the name of the field.
  
  To change the resolution strategy you can use the `resolution` parameter of the `@ScenarioState` annotations. For example, to use name instead of type resolution you can write
  
  `@ScenarioState(resolution = Resolution.NAME)`.
  
  ==== Value Validation
  By default, JGiven will not validate whether the value of a field of a stage that expects a value,
  was actually provided by a previous stage.
  The reason for this is that typically not all fields are always required for all steps.
  There might be scenarios where only a part of the fields are really necessary for the steps of the scenario.
  However, sometimes you know that a certain field value is needed for *all* steps of a stage.
  In this case you can set the `required` attribute of the `@ScenarioState` or `@ExpectedScenarioState` annotation to `true`.
  JGiven will then validate that a previous stage had provided the value and will throw an exception otherwise.
  
  === Having More Than 3 Stages
  
  