Skip to content

Commit 62de279

Browse files
committed
Initial migration of SystemPropertyExtension
Needs rebase after merge of LocaleExtension
1 parent b822a12 commit 62de279

File tree

14 files changed

+2938
-1335
lines changed

14 files changed

+2938
-1335
lines changed

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

Lines changed: 1022 additions & 1335 deletions
Large diffs are not rendered by default.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
import org.junit.jupiter.api.BeforeAll;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.ClassOrderer;
18+
import org.junit.jupiter.api.Nested;
19+
import org.junit.jupiter.api.Order;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.TestClassOrder;
22+
import org.junit.jupiter.api.TestInstance;
23+
import org.junit.jupiter.api.system.ClearSystemProperty;
24+
import org.junit.jupiter.api.system.ReadsSystemProperty;
25+
import org.junit.jupiter.api.system.RestoreSystemProperties;
26+
import org.junit.jupiter.api.system.SetSystemProperty;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
30+
public class SystemPropertyExtensionDemo {
31+
32+
// tag::systemproperty_clear_simple[]
33+
@Test
34+
@ClearSystemProperty(key = "some property")
35+
void testClearingProperty() {
36+
assertThat(System.getProperty("some property")).isNull();
37+
}
38+
// end::systemproperty_clear_simple[]
39+
40+
// tag::systemproperty_set_simple[]
41+
@Test
42+
@SetSystemProperty(key = "some property", value = "new value")
43+
void testSettingProperty() {
44+
assertThat(System.getProperty("some property")).isEqualTo("new value");
45+
}
46+
// end::systemproperty_set_simple[]
47+
48+
// tag::systemproperty_using_set_and_clear[]
49+
@Test
50+
@ClearSystemProperty(key = "1st property")
51+
@ClearSystemProperty(key = "2nd property")
52+
@SetSystemProperty(key = "3rd property", value = "new value")
53+
void testClearingAndSettingProperty() {
54+
assertThat(System.getProperty("1st property")).isNull();
55+
assertThat(System.getProperty("2nd property")).isNull();
56+
assertThat(System.getProperty("3rd property")).isEqualTo("new value");
57+
}
58+
// end::systemproperty_using_set_and_clear[]
59+
60+
@Nested
61+
// tag::systemproperty_using_at_class_level[]
62+
@ClearSystemProperty(key = "some property")
63+
class MySystemPropertyTest {
64+
65+
@Test
66+
@SetSystemProperty(key = "some property", value = "new value")
67+
void clearedAtClasslevel() {
68+
assertThat(System.getProperty("some property")).isEqualTo("new value");
69+
}
70+
71+
}
72+
// end::systemproperty_using_at_class_level[]
73+
74+
// tag::systemproperty_restore_test[]
75+
@ParameterizedTest
76+
@ValueSource(strings = { "foo", "bar" })
77+
@RestoreSystemProperties
78+
void parameterizedTest(String value) {
79+
System.setProperty("some parameterized property", value);
80+
System.setProperty("some other dynamic property", "my code calculates somehow");
81+
}
82+
// end::systemproperty_restore_test[]
83+
84+
@Nested
85+
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
86+
class SystemPropertyRestoreExample {
87+
88+
@Nested
89+
@Order(1)
90+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
91+
// tag::systemproperty_class_restore_setup[]
92+
@RestoreSystemProperties
93+
class MySystemPropertyRestoreTest {
94+
95+
@BeforeAll
96+
void beforeAll() {
97+
System.setProperty("A", "A value");
98+
}
99+
100+
@BeforeEach
101+
void beforeEach() {
102+
System.setProperty("B", "B value");
103+
}
104+
105+
@Test
106+
void isolatedTest1() {
107+
System.setProperty("C", "C value");
108+
}
109+
110+
@Test
111+
void isolatedTest2() {
112+
assertThat(System.getProperty("A")).isEqualTo("A value");
113+
assertThat(System.getProperty("B")).isEqualTo("B value");
114+
115+
// Class-level @RestoreSystemProperties restores "C" to original state
116+
assertThat(System.getProperty("C")).isNull();
117+
}
118+
119+
}
120+
// end::systemproperty_class_restore_setup[]
121+
122+
@Nested
123+
@Order(2)
124+
// tag::systemproperty_class_restore_isolated_class[]
125+
@ReadsSystemProperty
126+
class SomeOtherTestClass {
127+
128+
@Test
129+
void isolatedTest() {
130+
assertThat(System.getProperty("A")).isNull();
131+
assertThat(System.getProperty("B")).isNull();
132+
assertThat(System.getProperty("C")).isNull();
133+
}
134+
135+
}
136+
137+
// end::systemproperty_class_restore_isolated_class[]
138+
}
139+
140+
// tag::systemproperty_method_combine_all_test[]
141+
@ParameterizedTest
142+
@ValueSource(ints = { 100, 500, 1000 })
143+
@RestoreSystemProperties
144+
@SetSystemProperty(key = "DISABLE_CACHE", value = "TRUE")
145+
@ClearSystemProperty(key = "COPYWRITE_OVERLAY_TEXT")
146+
void imageGenerationTest(int imageSize) {
147+
System.setProperty("IMAGE_SIZE", String.valueOf(imageSize)); // Requires restore
148+
149+
// Test your image generation utility with the current system properties
150+
}
151+
// end::systemproperty_method_combine_all_test[]
152+
153+
}

junit-jupiter-api/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
exports org.junit.jupiter.api.timeout to org.junit.jupiter.engine;
3434

3535
opens org.junit.jupiter.api.condition to org.junit.platform.commons;
36+
exports org.junit.jupiter.api.system to org.junit.jupiter.engine;
3637
}

0 commit comments

Comments
 (0)