diff --git a/spock-core/src/main/java/org/spockframework/runtime/PlatformSpecRunner.java b/spock-core/src/main/java/org/spockframework/runtime/PlatformSpecRunner.java index de38bb012f..211ad6291c 100644 --- a/spock-core/src/main/java/org/spockframework/runtime/PlatformSpecRunner.java +++ b/spock-core/src/main/java/org/spockframework/runtime/PlatformSpecRunner.java @@ -255,6 +255,7 @@ void runParameterizedFeature(SpockExecutionContext context, ParameterizedFeature void runInitializer(SpockExecutionContext context) { getSpecificationContext(context).setCurrentFeature(context.getCurrentFeature()); + getSpecificationContext(context).setCurrentIteration(context.getCurrentIteration()); runInitializer(context, context.getSpec()); } diff --git a/spock-core/src/main/java/org/spockframework/runtime/SpecificationContext.java b/spock-core/src/main/java/org/spockframework/runtime/SpecificationContext.java index e602c5bf20..3bc4a307c2 100644 --- a/spock-core/src/main/java/org/spockframework/runtime/SpecificationContext.java +++ b/spock-core/src/main/java/org/spockframework/runtime/SpecificationContext.java @@ -50,7 +50,7 @@ public void setCurrentFeature(FeatureInfo currentFeature) { @Override public IterationInfo getCurrentIteration() { if (currentIteration == null) { - throw new IllegalStateException("Cannot request current iteration in @Shared context"); + throw new IllegalStateException("Cannot request current iteration in @Shared context, or feature context"); } return currentIteration; } diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/Interceptors.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/Interceptors.groovy index a46de5c676..46a6fd723e 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/Interceptors.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/Interceptors.groovy @@ -246,6 +246,12 @@ class FooSpec extends Specification { } catch (IllegalStateException ise) { assert ise.message == 'Cannot request current feature in @Shared context' } + try { + currentIteration + assert false: 'currentIteration should not be set' + } catch (IllegalStateException ise) { + assert ise.message == 'Cannot request current iteration in @Shared context, or feature context' + } } } } @@ -268,6 +274,12 @@ class FooSpec extends Specification { } catch (IllegalStateException ise) { assert ise.message == 'Cannot request current feature in @Shared context' } + try { + currentIteration + assert false: 'currentIteration should not be set' + } catch (IllegalStateException ise) { + assert ise.message == 'Cannot request current iteration in @Shared context, or feature context' + } } } } @@ -285,6 +297,12 @@ class FooSpec extends Specification { instance.specificationContext.with { assert currentSpec assert currentFeature + try { + currentIteration + assert false: 'currentIteration should not be set' + } catch (IllegalStateException ise) { + assert ise.message == 'Cannot request current iteration in @Shared context, or feature context' + } } } } @@ -302,6 +320,7 @@ class FooSpec extends Specification { instance.specificationContext.with { assert currentSpec assert currentFeature + assert currentIteration } } } @@ -319,6 +338,7 @@ class FooSpec extends Specification { instance.specificationContext.with { assert currentSpec assert currentFeature + assert currentIteration } } }