Best Testcontainers-java code snippet using org.testcontainers.containers.SpannerEmulatorContainer
Source:AbstractIntegrationTest.java
...12import org.springframework.test.context.ActiveProfiles;13import org.springframework.test.context.DynamicPropertyRegistry;14import org.springframework.test.context.DynamicPropertySource;15import org.springframework.test.web.servlet.MockMvc;16import org.testcontainers.containers.SpannerEmulatorContainer;17import org.testcontainers.junit.jupiter.Container;18import org.testcontainers.junit.jupiter.Testcontainers;19import org.testcontainers.utility.DockerImageName;20@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)21@Import({TestSpannerConfiguration.class})22@ActiveProfiles("integration")23@Testcontainers24@DirtiesContext25@AutoConfigureMockMvc26public abstract class AbstractIntegrationTest {27 static final String QUERY_SEPARATOR = ";";28 static final String SPANNER_EMULATOR_IMAGE = "gcr.io/cloud-spanner-emulator/emulator:1.1.1";29 static final String SPANNER_EMULATOR_HOST = "spring.cloud.gcp.spanner.emulator-host";30 private static final String DDL_CREATION_FILE = "sql/schema.sql";31 private static final String DDL_DROPPING_FILE = "sql/cleanup.sql";32 @Container33 private static final SpannerEmulatorContainer spannerEmulator =34 new SpannerEmulatorContainer(DockerImageName.parse(SPANNER_EMULATOR_IMAGE));35 @DynamicPropertySource36 private static void emulatorProperties(DynamicPropertyRegistry registry) {37 registry.add(SPANNER_EMULATOR_HOST, spannerEmulator::getEmulatorGrpcEndpoint);38 }39 @Autowired40 MockMvc mockMvc;41 @Autowired42 SpannerDatabaseAdminTemplate spannerDatabaseAdminTemplate;43 void createTables() {44 List<String> ddl = Stream45 .of(FileUtils.readFileAsString(DDL_CREATION_FILE).split(QUERY_SEPARATOR))46 .collect(toList());47 spannerDatabaseAdminTemplate.executeDdlStrings(ddl, true);48 }...
Source:SpannerEmulatorContainer.java
...5 * A Spanner container. Default ports: 9010 for GRPC and 9020 for HTTP.6 *7 * @author Eddú Meléndez8 */9public class SpannerEmulatorContainer extends GenericContainer<SpannerEmulatorContainer> {10 private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator");11 private static final int GRPC_PORT = 9010;12 private static final int HTTP_PORT = 9020;13 public SpannerEmulatorContainer(final DockerImageName dockerImageName) {14 super(dockerImageName);15 dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);16 withExposedPorts(GRPC_PORT, HTTP_PORT);17 setWaitStrategy(new LogMessageWaitStrategy()18 .withRegEx(".*Cloud Spanner emulator running\\..*"));19 }20 /**21 * @return a <code>host:port</code> pair corresponding to the address on which the emulator's22 * gRPC endpoint is reachable from the test host machine. Directly usable as a parameter to the23 * com.google.cloud.spanner.SpannerOptions.Builder#setEmulatorHost(java.lang.String) method.24 */25 public String getEmulatorGrpcEndpoint() {26 return getContainerIpAddress() + ":" + getMappedPort(GRPC_PORT);27 }...
SpannerEmulatorContainer
Using AI Code Generation
1import com.google.cloud.spanner.DatabaseAdminClient;2import com.google.cloud.spanner.DatabaseClient;3import com.google.cloud.spanner.DatabaseId;4import com.google.cloud.spanner.Spanner;5import com.google.cloud.spanner.SpannerOptions;6import com.google.cloud.spanner.testing.EmulatorSpannerHelper;7import org.junit.jupiter.api.Test;8import org.testcontainers.containers.SpannerEmulatorContainer;9public class SpannerEmulatorContainerTest {10 public void test() {11 SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();12 emulator.start();13 SpannerOptions options = SpannerOptions.newBuilder().setHost(emulator.getEmulatorGrpcEndpoint()).build();14 Spanner spanner = options.getService();15 DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();16 DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), "test-instance", "test-database"));17 spanner.close();18 emulator.stop();19 }20}
SpannerEmulatorContainer
Using AI Code Generation
1import com.google.cloud.spanner.Database;2import com.google.cloud.spanner.DatabaseAdminClient;3import com.google.cloud.spanner.DatabaseId;4import com.google.cloud.spanner.Spanner;5import com.google.cloud.spanner.SpannerOptions;6import com.google.cloud.spanner.testing.EmulatorSpannerHelper;7import java.util.Arrays;8import java.util.Collections;9import java.util.List;10import org.junit.After;11import org.junit.Before;12import org.junit.Test;13import org.testcontainers.containers.SpannerEmulatorContainer;14public class SpannerEmulatorContainerTest {15 private SpannerEmulatorContainer emulator;16 private Spanner spanner;17 public void setUp() {18 emulator = new SpannerEmulatorContainer();19 emulator.start();20 spanner = EmulatorSpannerHelper.getOptions(emulator.getEmulatorEndpoint()).getService();21 }22 public void tearDown() {23 spanner.close();24 emulator.stop();25 }26 public void testSpannerEmulator() {27 DatabaseAdminClient dbClient = spanner.getDatabaseAdminClient();28 DatabaseId db = DatabaseId.of("test-project", "test-instance", "test-db");29 dbClient.createDatabase(30 db.getInstanceId().getInstance(),31 db.getDatabase(),32 Collections.emptyList()).waitFor();33 Database database = dbClient.getDatabase(db.getInstanceId().getInstance(), db.getDatabase());34 System.out.println(database);35 }36}37import com.google.cloud.spanner.Database;38import com.google.cloud.spanner.DatabaseAdminClient;39import com.google.cloud.spanner.DatabaseId;40import com.google.cloud.spanner.Spanner;41import com.google.cloud.spanner.SpannerOptions;42import com.google.cloud.spanner.testing.EmulatorSpannerHelper;43import java.util.Arrays;44import java.util.Collections;45import java.util.List;46import org.junit.After;47import org.junit.Before;48import org.junit.Test;49import org.testcontainers.containers.SpannerEmulatorContainer;50public class SpannerEmulatorContainerTest {51 private SpannerEmulatorContainer emulator;52 private Spanner spanner;53 public void setUp() {54 emulator = new SpannerEmulatorContainer();55 emulator.start();56 spanner = EmulatorSpannerHelper.getOptions(emulator.getEmulatorEndpoint()).getService();57 }
SpannerEmulatorContainer
Using AI Code Generation
1import com.google.cloud.spanner.DatabaseAdminClient;2import com.google.cloud.spanner.DatabaseClient;3import com.google.cloud.spanner.DatabaseId;4import com.google.cloud.spanner.InstanceAdminClient;5import com.google.cloud.spanner.Spanner;6import com.google.cloud.spanner.SpannerOptions;7import org.junit.jupiter.api.AfterAll;8import org.junit.jupiter.api.BeforeAll;9import org.junit.jupiter.api.Test;10import org.testcontainers.containers.SpannerEmulatorContainer;11import org.testcontainers.utility.DockerImageName;12import java.util.Arrays;13import java.util.List;14public class SpannerEmulatorContainerTest {15 private static SpannerEmulatorContainer emulatorContainer;16 private static Spanner spanner;17 public static void setUp() {18 emulatorContainer = new SpannerEmulatorContainer(DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator:latest"));19 emulatorContainer.start();20 String host = emulatorContainer.getHost();21 int port = emulatorContainer.getPort();22 SpannerOptions options = SpannerOptions.newBuilder().setHost(host + ":" + port).build();23 spanner = options.getService();24 }25 public static void tearDown() {26 spanner.close();27 emulatorContainer.stop();28 }29 public void test() {30 InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();31 DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();32 String instanceId = "test-instance";33 String databaseId = "test-database";34 instanceAdminClient.createInstance(instanceId, instanceId, InstanceAdminClient.InstanceType.DEVELOPMENT, 1L);35 databaseAdminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE FOO (BAR INT64)"));36 DatabaseClient databaseClient = spanner.getDatabaseClient(DatabaseId.of(spanner.getOptions().getProjectId(), instanceId, databaseId));37 List<String> results = databaseClient.singleUse().executeQuery(com.google.cloud.spanner.Statement.of("SELECT 1")).getAll().get(0).getStringList(0);38 System.out.println(results);39 }40}
SpannerEmulatorContainer
Using AI Code Generation
1import com.google.cloud.spanner.DatabaseClient;2import com.google.cloud.spanner.DatabaseId;3import com.google.cloud.spanner.Spanner;4import com.google.cloud.spanner.SpannerOptions;5import org.junit.ClassRule;6import org.junit.Test;7import org.testcontainers.containers.SpannerEmulatorContainer;8public class SpannerEmulatorTest {9 public static SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();10 public void testSpanner() {11 SpannerOptions options = SpannerOptions.newBuilder()12 .setProjectId(emulator.getProjectId())13 .setHost(emulator.getHost())14 .setEmulatorHost(emulator.getEmulatorEndpoint())15 .build();16 Spanner spanner = options.getService();17 DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), emulator.getInstanceId(), emulator.getDatabaseId()));18 }19}
SpannerEmulatorContainer
Using AI Code Generation
1import com.google.cloud.spanner.*;2import org.testcontainers.containers.SpannerEmulatorContainer;3public class SpannerEmulatorContainerTest {4 public static void main(String[] args) {5 SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();6 emulator.start();7 SpannerOptions options = SpannerOptions.newBuilder()8 .setHost(emulator.getEmulatorEndpoint())9 .setProjectId(emulator.getProjectId())10 .setCredentials(emulator.getCredentials())11 .build();12 Spanner spanner = options.getService();13 DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), emulator.getInstanceId(), emulator.getDatabaseId()));14 client.readWriteTransaction().run(15 new TransactionRunner.TransactionCallable<Void>() {16 public Void run(TransactionContext transaction) throws Exception {17 long rowCount = transaction.executeUpdate(Statement.of("INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (1, 'FirstName1', 'LastName1')"));18 System.out.println(rowCount + " row(s) inserted");19 return null;20 }21 });22 spanner.close();23 emulator.stop();24 }25}261 row(s) inserted
SpannerEmulatorContainer
Using AI Code Generation
1import org.testcontainers.containers.SpannerEmulatorContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import java.io.IOException;4import java.io.InputStream;5import java.util.Properties;6import java.util.logging.Level;7import java.util.logging.Logger;8import java.util.stream.Collectors;9import com.google.auth.oauth2.GoogleCredentials;10import com.google.cloud.spanner.DatabaseClient;11import com.google.cloud.spanner.DatabaseId;12import com.google.cloud.spanner.InstanceId;13import com.google.cloud.spanner.Key;14import com.google.cloud.spanner.KeySet;15import com.google.cloud.spanner.Mutation;16import com.google.cloud.spanner.ResultSet;17import com.google.cloud.spanner.Spanner;18import com.google.cloud.spanner.SpannerOptions;19import com.google.cloud.spanner.Statement;20public class SpannerEmulatorTest {21 public static void main(String[] args) throws IOException {22 String projectId = "test-project";23 String instanceId = "test-instance";24 String databaseId = "test-database";25 SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer("gcr.io/cloud-spanner-emulator/emulator:latest");26 spannerEmulatorContainer.waitingFor(Wait.forHttp("/").forPort(9010));27 spannerEmulatorContainer.start();28 System.out.println("Spanner emulator started");29 System.out.println("Host: " + spannerEmulatorContainer.getHost());30 System.out.println("Port: " + spannerEmulatorContainer.getMappedPort(9010));31 System.out.println("Emulator endpoint: " + spannerEmulatorContainer.getEmulatorEndpoint());32 System.out.println("Admin endpoint: " + spannerEmulatorContainer.getAdminEndpoint());33 System.out.println("DatabaseAdmin endpoint: " + spannerEmulatorContainer.getDatabaseAdminEndpoint());34 System.out.println("InstanceAdmin endpoint: " + spannerEmulatorContainer.getInstanceAdminEndpoint());35 System.out.println("Spanner emulator started");36 System.out.println("Host: " + spannerEmulatorContainer.getHost());37 System.out.println("Port: " + spannerEmulatorContainer.getMappedPort(9010));38 System.out.println("Emulator endpoint: " + spannerEmulatorContainer.getEmulatorEndpoint());39 System.out.println("Admin endpoint: " + spannerEmulatorContainer.getAdminEndpoint());40 System.out.println("DatabaseAdmin endpoint: " + spannerEmulator
SpannerEmulatorContainer
Using AI Code Generation
1package com.google.cloud.spanner;2import com.google.cloud.spanner.connection.ConnectionOptions;3import com.google.cloud.spanner.connection.ConnectionOptions.SpannerOptionsBuilder;4import com.google.cloud.spanner.connection.SpannerPool;5import java.util.ArrayList;6import java.util.List;7import org.junit.AfterClass;8import org.junit.BeforeClass;9import org.junit.Test;10import org.testcontainers.containers.SpannerEmulatorContainer;11public class SpannerEmulatorTest {12 private static final SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();13 public static void startEmulator() {14 emulator.start();15 }16 public static void stopEmulator() {17 emulator.stop();18 }19 public void testSpannerEmulator() {20 SpannerOptionsBuilder builder = ConnectionOptions.newBuilder();21 builder.setHost(emulator.getEmulatorEndpoint());22 List<String> scopes = new ArrayList<>();23 builder.setScopes(scopes);24 SpannerPoolOptions options = SpannerPoolOptions.newBuilder().build();25 SpannerPool pool = SpannerPool.newBuilder().setSpannerOptions(builder.build()).setOptions(options).build();26 Spanner spanner = pool.getSpanner();27 spanner.close();28 }29}30package com.google.cloud.spanner;31import com.google.cloud.spanner.connection.ConnectionOptions;32import com.google.cloud.spanner.connection.ConnectionOptions.SpannerOptionsBuilder;33import com.google.cloud.spanner.connection.SpannerPool;34import java.util.ArrayList;35import java.util.List;36import org.junit.AfterClass;37import org.junit.BeforeClass;38import org.junit.Test;39import org.testcontainers.containers.SpannerEmulatorContainer;40public class SpannerEmulatorTest {41 private static final SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();42 public static void startEmulator() {43 emulator.start();44 }45 public static void stopEmulator() {46 emulator.stop();47 }48 public void testSpannerEmulator() {49 SpannerOptionsBuilder builder = ConnectionOptions.newBuilder();50 builder.setHost(emulator.getEmulatorEndpoint());51 List<String> scopes = new ArrayList<>();
SpannerEmulatorContainer
Using AI Code Generation
1import com.google.cloud.spanner.DatabaseClient;2import com.google.cloud.spanner.DatabaseId;3import com.google.cloud.spanner.Spanner;4import com.google.cloud.spanner.SpannerOptions;5import org.junit.jupiter.api.Test;6import org.testcontainers.containers.SpannerEmulatorContainer;7import java.util.List;8import static org.junit.jupiter.api.Assertions.assertEquals;9public class SpannerEmulatorTest {10 public void testSpannerEmulator() throws Exception {11 SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();12 emulator.start();13 SpannerOptions options = SpannerOptions.newBuilder()14 .setHost(emulator.getEmulatorEndpoint())15 .build();16 Spanner spanner = options.getService();17 DatabaseId db = DatabaseId.of(options.getProjectId(), emulator.getInstanceId(), emulator.getDatabaseId());18 DatabaseClient client = spanner.getDatabaseClient(db);19 List<String> names = client.singleUse().executeQuery(com.google.cloud.spanner.Statement.of("SELECT 'Hello World!'")).map(row -> row.getString(0)).list();20 assertEquals("Hello World!", names.get(0));21 spanner.close();22 }23}
SpannerEmulatorContainer
Using AI Code Generation
1import org.testcontainers.containers.SpannerEmulatorContainer;2import org.testcontainers.utility.DockerImageName;3public class SpannerEmulatorContainerUsage {4 public static void main(String[] args) {5 final SpannerEmulatorContainer emulator = new SpannerEmulatorContainer(DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator:latest"));6 emulator.start();7 System.out.println(emulator.getEmulatorEndpoint());8 emulator.stop();9 emulator.start();10 System.out.println(emulator.getEmulatorEndpoint());11 emulator.stop();12 }13}14SpannerEmulatorContainer(DockerImageName dockerImageName)15getEmulatorEndpoint()16getEmulatorGrpcEndpoint()17getEmulatorRestEndpoint()18getProjectId()19getHost()20getPort()21getDatabase()22getCredentials()23getCredentialsProvider()24getOptions()25getSpanner()26getDatabaseClient()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!