Skip to content

Commit 7b494c6

Browse files
JoeWang1127lqiu96
authored andcommitted
chore: add junit5 extension to setup environment variable (#2799)
Fix: #2785.
1 parent dae1ba9 commit 7b494c6

File tree

8 files changed

+27
-81
lines changed

8 files changed

+27
-81
lines changed

.github/workflows/ci.yaml

+8-35
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ jobs:
2222
run: |
2323
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
2424
-Dfmt.skip -DenableTestCoverage
25-
# The `envVarTest` profile runs tests that require an environment variable
26-
- name: Env Var Tests
27-
run: |
28-
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
29-
-Dfmt.skip -DenableTestCoverage -PenvVarTest
30-
# Set the Env Var for this step only
31-
env:
32-
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
3325
- run: bazelisk version
3426
- name: Install Maven modules
3527
run: |
@@ -71,15 +63,7 @@ jobs:
7163
- name: Unit Tests
7264
run: |
7365
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
74-
-Dfmt.skip -DenableTestCoverage
75-
# The `envVarTest` profile runs tests that require an environment variable
76-
- name: Env Var Tests
77-
run: |
78-
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
79-
-Dfmt.skip -DenableTestCoverage -PenvVarTest
80-
# Set the Env Var for this step only
81-
env:
82-
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
66+
8367
- run: bazelisk version
8468
- name: Install Maven modules
8569
run: |
@@ -111,25 +95,14 @@ jobs:
11195
export PATH=${JAVA_HOME}/bin:$PATH
11296
# Maven surefire plugin lets us to specify the JVM when running tests via
11397
# the "jvm" system property.
114-
mvn verify --batch-mode --no-transfer-progress -Dcheckstyle.skip \
98+
mvn org.apache.maven.plugins:maven-surefire-plugin:test \
99+
verify \
100+
--batch-mode \
101+
--no-transfer-progress \
102+
-Dcheckstyle.skip \
115103
-Dfmt.skip \
116-
-Djvm="${JAVA8_HOME}/bin/java"
117-
# The `envVarTest` profile runs tests that require an environment variable
118-
- name: Compile with Java 17 and run tests with Java 8 (Env Var Tests)
119-
shell: bash
120-
run: |
121-
set -x
122-
export JAVA_HOME=$JAVA_HOME
123-
export PATH=${JAVA_HOME}/bin:$PATH
124-
# Maven surefire plugin lets us to specify the JVM when running tests via
125-
# the "jvm" system property.
126-
export GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com
127-
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
128-
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
129-
-PenvVarTest
130-
# Set the Env Var for this step only
131-
env:
132-
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
104+
-Djvm="${JAVA8_HOME}/bin/java" \
105+
-DargLine="-Djava.util.logging.SimpleFormatter.format='%1$tY %1$tl:%1$tM:%1$tS.%1$tL %2$s %4$s: %5$s%6$s%n'"
133106
134107
build-java8-gapic-generator-java:
135108
name: "build(8) for gapic-generator-java"

.github/workflows/sonar.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ jobs:
4747
tar -xf showcase-*
4848
./gapic-showcase run &
4949
cd -
50-
# Intentionally do not run the Env Var Tests (no -PenvVarTests) as setting the Env Var
51-
# may alter the results for other tests that use Env Var in the logic. Adding a Sonar
52-
# step for a few tests (env var tests) may be overkill and should be better covered
53-
# when we can upgrade to JUnit 5 (https://github.com/googleapis/sdk-platform-java/issues/1611#issuecomment-1970079325)
5450
- name: Build and analyze for full test coverage
5551
env:
5652
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any

gapic-generator-java-pom-parent/pom.xml

-17
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,6 @@
182182
</plugins>
183183
</build>
184184
</profile>
185-
<profile>
186-
<id>envVarTest</id>
187-
<build>
188-
<plugins>
189-
<plugin>
190-
<groupId>org.apache.maven.plugins</groupId>
191-
<artifactId>maven-surefire-plugin</artifactId>
192-
<configuration>
193-
<!-- Unless overriden in the submodules, this profile run no tests in a submodule -->
194-
<excludes>
195-
<exclude>**/*.java</exclude>
196-
</excludes>
197-
</configuration>
198-
</plugin>
199-
</plugins>
200-
</build>
201-
</profile>
202185
</profiles>
203186
<repositories>
204187
<repository>

gax-java/dependencies.properties

+1
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ maven.net_bytebuddy_byte_buddy=net.bytebuddy:byte-buddy:1.14.16
8888
maven.org_objenesis_objenesis=org.objenesis:objenesis:2.6
8989
maven.org_junit_jupiter_junit_jupiter_api=org.junit.jupiter:junit-jupiter-api:5.10.2
9090
maven.org_junit_jupiter_junit_jupiter_params=org.junit.jupiter:junit-jupiter-params:5.10.2
91+
maven.org_junit_pioneer_junit_pioneer=org.junit-pioneer:junit-pioneer:2.2.0

gax-java/gax/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ _TEST_COMPILE_DEPS = [
4141
"@net_bytebuddy_byte_buddy//jar",
4242
"@org_objenesis_objenesis//jar",
4343
"@com_googlecode_java_diff_utils_diffutils//jar",
44+
"@org_junit_pioneer_junit_pioneer//jar",
4445
]
4546

4647
java_library(

gax-java/gax/pom.xml

+8-22
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,18 @@
9999
</configuration>
100100
</plugin>
101101
<plugin>
102-
<!-- Troubleshooting a flaky test in https://github.com/googleapis/sdk-platform-java/issues/1931 -->
103102
<groupId>org.apache.maven.plugins</groupId>
104103
<artifactId>maven-surefire-plugin</artifactId>
105104
<configuration>
106-
<argLine>-Djava.util.logging.SimpleFormatter.format="%1$tY %1$tl:%1$tM:%1$tS.%1$tL %2$s %4$s: %5$s%6$s%n"</argLine>
107-
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
108-
<test>!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet+endpointContextBuild_multipleUniverseDomainConfigurations_clientSettingsHasPriority</test>
105+
<argLine>
106+
-Djava.util.logging.SimpleFormatter.format="%1$tY %1$tl:%1$tM:%1$tS.%1$tL %2$s %4$s: %5$s%6$s%n"
107+
<!-- workaround setup for using junit-pioneer with Java 17 or above,
108+
see https://junit-pioneer.org/docs/environment-variables/#warnings-for-reflective-access -->
109+
--add-opens java.base/java.util=ALL-UNNAMED
110+
--add-opens java.base/java.lang=ALL-UNNAMED
111+
</argLine>
109112
</configuration>
110113
</plugin>
111114
</plugins>
112115
</build>
113-
114-
<profiles>
115-
<profile>
116-
<id>envVarTest</id>
117-
<build>
118-
<plugins>
119-
<plugin>
120-
<groupId>org.apache.maven.plugins</groupId>
121-
<artifactId>maven-surefire-plugin</artifactId>
122-
<configuration>
123-
<test>EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet+endpointContextBuild_multipleUniverseDomainConfigurations_clientSettingsHasPriority</test>
124-
</configuration>
125-
</plugin>
126-
</plugins>
127-
</build>
128-
</profile>
129-
</profiles>
130-
</project>
116+
</project>

gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.io.IOException;
4141
import org.junit.jupiter.api.BeforeEach;
4242
import org.junit.jupiter.api.Test;
43+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
4344
import org.mockito.Mockito;
4445

4546
class EndpointContextTest {
@@ -337,8 +338,8 @@ void endpointContextBuild_gdchFlow_noUniverseDomain_customEndpoint() throws IOEx
337338

338339
// This Universe Domain should match the `GOOGLE_CLOUD_UNIVERSE_DOMAIN` Env Var
339340
// For this test running locally or in CI, check that the Env Var is set properly.
340-
// This test should only run when the maven profile `EnvVarTest` is enabled.
341341
@Test
342+
@SetEnvironmentVariable(key = EndpointContext.GOOGLE_CLOUD_UNIVERSE_DOMAIN, value = "random.com")
342343
void endpointContextBuild_universeDomainEnvVarSet() throws IOException {
343344
String envVarUniverseDomain = "random.com";
344345
EndpointContext endpointContext =
@@ -352,11 +353,10 @@ void endpointContextBuild_universeDomainEnvVarSet() throws IOException {
352353

353354
// This Universe Domain should match the `GOOGLE_CLOUD_UNIVERSE_DOMAIN` Env Var
354355
// For this test running locally or in CI, check that the Env Var is set properly.
355-
// This test should only run when the maven profile `EnvVarTest` is enabled.
356356
@Test
357+
@SetEnvironmentVariable(key = EndpointContext.GOOGLE_CLOUD_UNIVERSE_DOMAIN, value = "random.com")
357358
void endpointContextBuild_multipleUniverseDomainConfigurations_clientSettingsHasPriority()
358359
throws IOException {
359-
// This test has `GOOGLE_CLOUD_UNIVERSE_DOMAIN` = `random.com`
360360
String clientSettingsUniverseDomain = "clientSettingsUniverseDomain.com";
361361
EndpointContext endpointContext =
362362
defaultEndpointContextBuilder

gax-java/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@
211211
</exclusions>
212212
<scope>test</scope>
213213
</dependency>
214+
<dependency>
215+
<groupId>org.junit-pioneer</groupId>
216+
<artifactId>junit-pioneer</artifactId>
217+
<version>2.2.0</version>
218+
<scope>test</scope>
219+
</dependency>
214220
</dependencies>
215221

216222
<build>

0 commit comments

Comments
 (0)