Skip to content

Commit 034a92f

Browse files
committed
chore: fixing the unit test environment
change mocking GCP environment config from using MonitoredResourceUtil to using fake system property.
1 parent a5ff479 commit 034a92f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class LoggingHandlerTest {
5353
private static final String LOG_NAME = "java.log";
5454
private static final String MESSAGE = "message";
5555
private static final String PROJECT = "project";
56+
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";
5657

5758
private static final MonitoredResource DEFAULT_RESOURCE =
5859
MonitoredResource.of("global", ImmutableMap.of("project_id", PROJECT));
@@ -224,15 +225,15 @@ private static LogRecord newLogRecord(Level level, String message) {
224225

225226
@Test
226227
public void testDefaultHandlerCreation() {
227-
// mocks behavior of MonitoredResourceUtil which is used by LoggingOptions.getDefaultInstance()
228-
// static method
229-
ResourceTypeEnvironmentGetter getterMock =
230-
EasyMock.createMock(ResourceTypeEnvironmentGetter.class);
231-
expect(getterMock.getAttribute("project/project-id")).andReturn(PROJECT);
232-
expect(getterMock.getAttribute("")).andStubReturn(null);
233-
MonitoredResourceUtil.setEnvironmentGetter(getterMock);
234-
replay(options, logging, getterMock);
228+
String oldProject = System.getProperty(PROJECT_ENV_NAME);
229+
System.setProperty(PROJECT_ENV_NAME, PROJECT);
230+
replay(options, logging);
235231
assertNotNull(new LoggingHandler());
232+
if (oldProject != null) {
233+
System.setProperty(PROJECT_ENV_NAME, oldProject);
234+
} else {
235+
System.clearProperty(PROJECT_ENV_NAME);
236+
}
236237
}
237238

238239
@Test

0 commit comments

Comments
 (0)