Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Test support module #2357

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions docs/src/main/asciidoc/test.adoc
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`.
Copy link
Contributor

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 ""?

Copy link
Contributor Author

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?

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
}
----
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<module>spring-cloud-gcp-bigquery</module>
<module>spring-cloud-gcp-security-firebase</module>
<module>spring-cloud-gcp-secretmanager</module>
<module>spring-cloud-gcp-test</module>
</modules>

<properties>
Expand Down
1 change: 0 additions & 1 deletion spring-cloud-gcp-data-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<groupId>org.springframework.cloud</groupId>
<version>1.2.3.BUILD-SNAPSHOT</version>
</parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-data-spanner</artifactId>
<name>Spring Cloud GCP Cloud Spanner Module</name>
<description>Spring Cloud GCP Cloud Spanner Module</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion spring-cloud-gcp-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@
<artifactId>spring-cloud-gcp-starter-secretmanager</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-test</artifactId>
<version>${project.version}</version>
</dependency>
<!-- spring-cloud-gcp-starter-sql -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-gcp-pubsub-stream-binder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
<version>3.1.0.BUILD-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

This file was deleted.

Loading