How to Mock Application Default Credentials in google.cloud.aiplatform.v1 using junit Java?

62 views Asked by At

I have been writing some unit tests for my application that makes use of google.cloud.aiplatform.v1

I know how to set up ADC via security json credentials file. As I understand, if the enviroment variable "GOOGLE_APPLICATION_CREDENTIALS" is not present in the system it throws the following error:

java.io.IOException: Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc.

Now here my issue. I want to write my unit tests independent of this environment variable. For that purpose I would like to mock the dependency call for ADC. I have tried mocking the "getDefaultCredentials" method in Credentials class but no luck. I am seeing the above error when running my unit tests.

@Before
public void setUp() throws Exception {
    // Mocking the behavior of GoogleCredentials.getApplicationDefault()
    // For example, if you want to return the mock credentials when getApplicationDefault() is called
    when(GoogleCredentials.getApplicationDefault()).thenReturn(googleCredentialsMock);


    configuration = new PropertiesConfiguration(VertexAIPredictorProvider.class.getResource("/test.properties"));
    retryLogic = new RetryLogic();
    vertexAIPredictorProvider = new VertexAIPredictorProvider(configuration, retryLogic);
}

@Test
public void testGetVertexAIPredictor() throws IOException {

    // this class throws the above specified error
    PredictorV2 predictorV2 = vertexAIPredictorProvider.getPredictor("vertexai");
    assertNotNull(predictorV2);
}

Can someone please point what I am doing wrong. Help would be appreciated thanks

0

There are 0 answers