-
Notifications
You must be signed in to change notification settings - Fork 682
Test support module #2357
base: main
Are you sure you want to change the base?
Test support module #2357
Changes from 12 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,108 @@ | ||
| == Emulators support | ||
| Our tools simplify starting and stopping Cloud PubSub and Cloud Spanner emulators when you test your application. | ||
|
|
||
| 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` and the emulators installed and configured, as described later. | ||
dmitry-s marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| === Cloud PubSub Emulator | ||
| In order to use our Cloud PubSub Emulator helper tools, you would need to install the emulator first. | ||
| Follow the https://cloud.google.com/pubsub/docs/emulator[installation instructions]. | ||
|
|
||
| ==== JUnit 4 Class Rule | ||
| The rule starts the emulator process before the tests run and kills it afterwards. | ||
dmitry-s marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| You just need to add a rule into your test class to enable it. | ||
|
|
||
| [source,java] | ||
| ---- | ||
| import org.springframework.cloud.gcp.test.PubSubEmulatorRule; | ||
|
|
||
| public class PubSubTests { | ||
| @ClassRule | ||
| public static PubSubEmulatorRule emulator = new PubSubEmulatorRule(); | ||
|
|
||
| //your tests | ||
| } | ||
| ---- | ||
|
|
||
| ==== Utility class | ||
| If you prefer controlling the emulator manually, you can use PubSubEmulatorHelper. | ||
|
|
||
| [source,java] | ||
| ---- | ||
| PubSubEmulatorHelper emulatorHelper = new PubSubEmulatorHelper(); | ||
|
|
||
| emulatorHelper.startEmulator(); | ||
|
|
||
| //your code | ||
|
|
||
| emulatorHelper.shutdownEmulator(); | ||
| ---- | ||
|
|
||
| === 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`. | ||
|
|
||
| ==== Spring Configuration | ||
dmitry-s marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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. | ||
dmitry-s marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
| } | ||
| ---- | ||
|
|
||
| ==== JUnit 4 Class Rule | ||
| If you don't use spring in your tests, you can use the class rule. | ||
|
|
||
| NOTE: The rule does not work with SpringRunner! | ||
| See <<Spring Configuration>> section instead. | ||
|
|
||
| [source,java] | ||
| ---- | ||
| import org.springframework.cloud.gcp.test.SpannerEmulatorRule; | ||
|
|
||
| public class SpannerTests { | ||
| @ClassRule | ||
| public static SpannerEmulatorRule emulator = new SpannerEmulatorRule(); | ||
|
|
||
| //your tests | ||
| } | ||
| ---- | ||
|
|
||
|
|
||
|
|
||
| ==== Utility class | ||
| If you prefer controlling the emulator manually, you can use SpannerEmulatorHelper. | ||
|
|
||
| [source,java] | ||
| ---- | ||
| SpannerEmulatorHelper emulatorHelper = new SpannerEmulatorHelper(); | ||
|
|
||
| emulatorHelper.startEmulator(); | ||
|
|
||
| //your code | ||
|
|
||
| emulatorHelper.shutdownEmulator(); | ||
| ---- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2017-2018 the original author or authors. | ||
|
||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.cloud.gcp.data.spanner.core.it; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import org.springframework.cloud.gcp.data.spanner.test.AbstractSpannerIntegrationTest; | ||
| import org.springframework.cloud.gcp.data.spanner.test.domain.Trade; | ||
| import org.springframework.cloud.gcp.test.SpannerEmulatorSpringConfiguration; | ||
| import org.springframework.test.context.ContextConfiguration; | ||
| import org.springframework.test.context.junit4.SpringRunner; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Integration tests that use many features of the Spanner Template. | ||
| * | ||
| * @author Balint Pato | ||
| * @author Chengyuan Zhao | ||
| */ | ||
| @RunWith(SpringRunner.class) | ||
| @ContextConfiguration(classes = {SpannerEmulatorSpringConfiguration.class}) | ||
| public class SpannerTemplateEmulatorTests extends AbstractSpannerIntegrationTest { | ||
|
|
||
| @Test | ||
| public void insertSingleRow() { | ||
|
|
||
| Trade trade = Trade.aTrade(null, 1); | ||
| this.spannerOperations.insert(trade); | ||
| assertThat(this.spannerOperations.count(Trade.class)).isEqualTo(1L); | ||
| } | ||
|
|
||
| } | ||
| 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 | ||
|
||
| 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(); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.