2525import com .google .auth .oauth2 .ServiceAccountCredentials ;
2626import com .google .cloud .spanner .ErrorCode ;
2727import com .google .cloud .spanner .SpannerException ;
28- import java .io .FileInputStream ;
28+ import java .io .File ;
2929import java .io .IOException ;
30+ import java .nio .file .Files ;
3031import org .junit .Test ;
3132import org .junit .runner .RunWith ;
3233import org .junit .runners .JUnit4 ;
3536@ RunWith (JUnit4 .class )
3637public class CredentialsServiceTest {
3738 private static final String FILE_TEST_PATH =
38- CredentialsServiceTest .class .getResource ("test-key.json" ).getFile ();
39- private static final String APP_DEFAULT_FILE_TEST_PATH =
40- CredentialsServiceTest .class .getResource ("test-key-app-default.json" ).getFile ();
39+ CredentialsServiceTest .class .getResource ("test-key.json" ).getPath ();
40+ private static final String SA_APP_DEFAULT_FILE_TEST_PATH =
41+ CredentialsServiceTest .class .getResource ("test-key-app-default.json" ).getPath ();
4142
4243 private static final String TEST_PROJECT_ID = "test-project" ;
4344 private static final String APP_DEFAULT_PROJECT_ID = "app-default-test-project" ;
@@ -49,7 +50,11 @@ public class CredentialsServiceTest {
4950 GoogleCredentials internalGetApplicationDefault () throws IOException {
5051 // Read application default credentials directly from a specific file instead of actually
5152 // fetching the default from the environment.
52- return GoogleCredentials .fromStream (new FileInputStream (APP_DEFAULT_FILE_TEST_PATH ));
53+ return ServiceAccountCredentials .fromStream (
54+ // Calling `getResource().getPath()` on Windows returns a string that might start with
55+ // something like `/C:/...`. Paths.get() interprets the leading / as part of the path
56+ // and would be invalid. Use `new File().toPath()` to read from these files.
57+ Files .newInputStream (new File (SA_APP_DEFAULT_FILE_TEST_PATH ).toPath ()));
5358 }
5459 };
5560
0 commit comments