-
Notifications
You must be signed in to change notification settings - Fork 693
Test support module #2357
base: main
Are you sure you want to change the base?
Test support module #2357
Changes from 19 commits
af58b0c
616fd6c
c97d2a5
e760570
ddd4fb7
6e9f411
2a51055
2ac9f78
9f848dc
4a80876
ef720e0
fe11788
7b119e7
8af2f8a
9d0f694
d043c0e
946914a
df4c69c
aa31b9d
dab420e
4d9fd10
e5e49b7
36cbc62
10a1019
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
== Emulators support | ||
Our tools simplify starting and stopping Cloud Pub/Sub and Cloud Spanner emulators when you test your applications locally. | ||
|
||
In order to use it, you need to add this dependency into your `pom.xml` file: | ||
|
||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-gcp-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
---- | ||
|
||
Also, you need to have `gcloud` CLI and the emulators installed and configured, as described later. | ||
|
||
=== JUnit 4 Class Rule | ||
The rule starts the emulator process before the tests run and stops it afterwards. | ||
You just need to add a rule into your test class to enable it. | ||
|
||
[source,java] | ||
---- | ||
import org.springframework.cloud.gcp.test.EmulatorRule; | ||
import org.springframework.cloud.gcp.test.pubsub.PubSubEmulator; | ||
|
||
public class PubSubTests { | ||
@ClassRule | ||
public static EmulatorRule emulator = new EmulatorRule(new PubSubEmulator()); | ||
|
||
//your tests | ||
} | ||
---- | ||
|
||
NOTE: The class rule doesn't work for `SpannerEmulator`. | ||
See <<Spring Configuration>> section instead. | ||
|
||
=== Utility class | ||
If you prefer controlling the emulator manually, you can use the `EmulatorDriver`. | ||
|
||
[source,java] | ||
---- | ||
EmulatorDriver emulatorDriver = new EmulatorDriver(new PubSubEmulator()); | ||
|
||
emulatorDriver.startEmulator(); | ||
|
||
//your code | ||
|
||
emulatorDriver.shutdownEmulator(); | ||
---- | ||
|
||
=== Cloud Pub/Sub Emulator | ||
In order to use our Cloud Pub/Sub Emulator helper tools, you would need to install the emulator first. | ||
Follow the https://cloud.google.com/pubsub/docs/emulator[installation instructions]. | ||
|
||
Use `new PubSubEmulator()` as an argument to `EmulatorDriver` and `EmulatorRule`. | ||
|
||
=== Cloud Spanner Emulator | ||
In order to use our Cloud Spanner Emulator helper tools, you would need to install the emulator first. | ||
Follow the https://cloud.google.com/spanner/docs/emulator[installation instructions]. | ||
Make sure you also create an https://cloud.google.com/spanner/docs/emulator#using_the_gcloud_cli_with_the_emulator[emulator configuration] and call it `emulator`. | ||
|
||
Use `new SpannerEmulator()` as an argument to `EmulatorDriver`. | ||
|
||
==== Spanner Emulator Spring Configuration | ||
If you are testing your Spring application, you can use our configuration class. | ||
It provides a bean of type `Spanner`, which starts the emulator before creating a client. | ||
This configuration also stops the emulator when the Spring context is shut down. | ||
|
||
In order to use it, you need to add the `SpannerEmulatorSpringConfiguration.class` to your configuration classes list in `@ContextConfiguration`. | ||
|
||
[source,java] | ||
---- | ||
import org.junit.runner.RunWith; | ||
|
||
import org.springframework.cloud.gcp.test.SpannerEmulatorSpringConfiguration; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ContextConfiguration(classes = {SpannerEmulatorSpringConfiguration.class, YourSpringConfiguration.class}) | ||
public class SpannerTemplateEmulatorTests { | ||
//your tests | ||
} | ||
---- |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import com.google.cloud.spanner.SpannerOptions; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.cloud.gcp.core.Credentials; | ||
import org.springframework.cloud.gcp.core.DefaultCredentialsProvider; | ||
import org.springframework.cloud.gcp.core.DefaultGcpProjectIdProvider; | ||
|
@@ -104,6 +105,7 @@ public TradeRepositoryTransactionalService tradeRepositoryTransactionalService() | |
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not sure remove these two bean definitions from here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, remove the bean definitions entirely? There are several integration tests that go against real spanner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use this configuration file. I removed these beans from the emulator's test spring configuration. |
||
public SpannerOptions spannerOptions() { | ||
return SpannerOptions.newBuilder().setProjectId(getProjectId()) | ||
.setSessionPoolOption(SessionPoolOptions.newBuilder().setMaxSessions(10).build()) | ||
|
@@ -116,6 +118,7 @@ public DatabaseId databaseId() { | |
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public Spanner spanner(SpannerOptions spannerOptions) { | ||
return spannerOptions.getService(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we mention the workaround of setting up a custom Spanner bean and setting its destroy method to ""?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like implementation details to me. @meltsufin what do you think?