Reputation: 14520
i'm using spring boot starter spring-cloud-gcp-starter-pubsub
and PubSubTemplate
. it works perfectly with actual GCP but now i want to use it for my tests with local Google PubSub emulator. how can i provide my custom url, port and no credentials?
or i have to revert back to using plain google sdk as in this example
EDIT:
i have only one bean in my app:
@Service
class TestLocalPubsub(pubSubTemplate: PubSubTemplate)
and
application.properties:
spring.cloud.gcp.pubsub.emulator-host=localhost:8085
and when i run the app i got:
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:132) ~[google-auth-library-oauth2-http-0.12.0.jar:na] ...
but of course the link says about gcloud sdk, not pubsubtemplate
Upvotes: 2
Views: 3371
Reputation: 2376
As mentioned in the post you linked, there are couple ways to get spring to connect to emulator:
SPRING_CLOUD_GCP_PUBSUB_EMULATORHOST=locahost:8085
(if using the default emulator portspring.cloud.gcp.pubsub.emulator-host=localhost:8085
in your Spring application propertiesFor tests, you can use the second method by creating an application.properties in test/main/resources and setting spring.cloud.gcp.pubsub.emulator-host=localhost:8085
. You shouldn't need to override any custom credentials.
In order for your tests to work with emulator, you need to create your topics and subscriptions in the emulator ahead of time. The GCP docs show how to do this manually by installing Python Google Cloud Client library: https://cloud.google.com/pubsub/docs/emulator. Otherwise your tests need to create them using the SDK.
Here is a complete example of using Spring with pubsub emulator and creating the topic/subscription using Java SDK: https://github.com/nhartner/pubsub-emulator-demo
Upvotes: 3