From 034a92f335cf98e2626eebb2d6f9b71a0ffc776c Mon Sep 17 00:00:00 2001 From: minherz Date: Mon, 17 Jan 2022 10:44:43 +0000 Subject: [PATCH] chore: fixing the unit test environment change mocking GCP environment config from using MonitoredResourceUtil to using fake system property. --- .../cloud/logging/LoggingHandlerTest.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index 7398e0308..dbe3f7f5f 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -53,6 +53,7 @@ public class LoggingHandlerTest { private static final String LOG_NAME = "java.log"; private static final String MESSAGE = "message"; private static final String PROJECT = "project"; + private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; private static final MonitoredResource DEFAULT_RESOURCE = MonitoredResource.of("global", ImmutableMap.of("project_id", PROJECT)); @@ -224,15 +225,15 @@ private static LogRecord newLogRecord(Level level, String message) { @Test public void testDefaultHandlerCreation() { - // mocks behavior of MonitoredResourceUtil which is used by LoggingOptions.getDefaultInstance() - // static method - ResourceTypeEnvironmentGetter getterMock = - EasyMock.createMock(ResourceTypeEnvironmentGetter.class); - expect(getterMock.getAttribute("project/project-id")).andReturn(PROJECT); - expect(getterMock.getAttribute("")).andStubReturn(null); - MonitoredResourceUtil.setEnvironmentGetter(getterMock); - replay(options, logging, getterMock); + String oldProject = System.getProperty(PROJECT_ENV_NAME); + System.setProperty(PROJECT_ENV_NAME, PROJECT); + replay(options, logging); assertNotNull(new LoggingHandler()); + if (oldProject != null) { + System.setProperty(PROJECT_ENV_NAME, oldProject); + } else { + System.clearProperty(PROJECT_ENV_NAME); + } } @Test