Skip to content

Commit afd1682

Browse files
authored
Fix @RestoreSystemProperties for iterations (#2111)
fixes #2104
1 parent 620f873 commit afd1682

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

docs/release_notes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ include::include.adoc[]
99

1010
* Fix ExtensionException in OSGi environment for global extension spockIssue:2076[]
1111
** This issue was introduced with spockPull:1995[]
12+
* Fix `@RestoreSystemProperties` not restoring state between iterations of a data-driven feature spockIssue:2104[]
1213

1314
== 2.4-M5 (2025-01-07)
1415

spock-core/src/main/java/org/spockframework/runtime/extension/builtin/RestoreSystemPropertiesExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public class RestoreSystemPropertiesExtension implements IStatelessAnnotationDri
3030
public void visitSpecAnnotation(RestoreSystemProperties annotation, SpecInfo spec) {
3131
spec.addExclusiveResource(EXCLUSIVE_RESOURCE);
3232
for (FeatureInfo feature : spec.getFeatures()) {
33-
feature.addInterceptor(RestoreSystemPropertiesInterceptor.INSTANCE);
33+
feature.addIterationInterceptor(RestoreSystemPropertiesInterceptor.INSTANCE);
3434
}
3535
}
3636

3737
@Override
3838
public void visitFeatureAnnotation(RestoreSystemProperties annotation, FeatureInfo feature) {
39-
feature.addInterceptor(RestoreSystemPropertiesInterceptor.INSTANCE);
39+
feature.addIterationInterceptor(RestoreSystemPropertiesInterceptor.INSTANCE);
4040
feature.addExclusiveResource(EXCLUSIVE_RESOURCE);
4141
}
4242
}

spock-specs/src/test/groovy/org/spockframework/smoke/extension/RestoreSystemPropertiesExtension.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ class RestoreSystemPropertiesExtension extends Specification {
3636
System.setProperty("RestoreSystemPropertiesExtension.prop2", "new value")
3737
}
3838

39+
@Issue("https://github.com/spockframework/spock/issues/2104")
40+
@RestoreSystemProperties
41+
def "restores properties for each iteration of a data-driven feature method"() {
42+
expect:
43+
System.getProperty("RestoreSystemPropertiesExtension.iteration") == null
44+
45+
when:
46+
System.setProperty("RestoreSystemPropertiesExtension.iteration", "$i")
47+
48+
then:
49+
System.getProperty("RestoreSystemPropertiesExtension.iteration") == "$i"
50+
51+
where:
52+
i << (1..3)
53+
}
54+
3955
def cleanupSpec() {
4056
assert System.getProperty("RestoreSystemPropertiesExtension.prop1") == "original value"
4157
assert System.getProperty("RestoreSystemPropertiesExtension.prop2") == "new value"

0 commit comments

Comments
 (0)