Best Testcontainers-java code snippet using org.testcontainers.containers.SpannerEmulatorContainer.getEmulatorGrpcEndpoint
Source:OrderServiceTest.java
...32 DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator:1.1.1"));33 @DynamicPropertySource34 static void emulatorProperties(DynamicPropertyRegistry registry) {35 registry.add(36 "spring.cloud.gcp.spanner.emulator-host", spannerEmulator::getEmulatorGrpcEndpoint);37 }38 @TestConfiguration39 static class EmulatorConfiguration {40 @Bean41 NoCredentialsProvider googleCredentials() {42 return NoCredentialsProvider.create();43 }44 }45 private InstanceId createInstance(Spanner spanner) throws InterruptedException, ExecutionException {46 InstanceConfigId instanceConfig = InstanceConfigId.of(PROJECT_ID, "emulator-config");47 InstanceId instanceId = InstanceId.of(PROJECT_ID, INSTANCE_ID);48 InstanceAdminClient insAdminClient = spanner.getInstanceAdminClient();49 Instance instance = insAdminClient50 .createInstance(51 InstanceInfo52 .newBuilder(instanceId)53 .setNodeCount(1)54 .setDisplayName("Test instance")55 .setInstanceConfigId(instanceConfig)56 .build()57 )58 .get();59 return instanceId;60 }61 private void createDatabase(Spanner spanner) throws InterruptedException, ExecutionException {62 DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();63 Database database = dbAdminClient64 .createDatabase(65 INSTANCE_ID,66 "order_schema",67 Arrays.asList("CREATE TABLE Orders (orderId INT64 NOT NULL, name STRING(255), order_status STRING(255)) PRIMARY KEY (orderId)")68 )69 .get();70 }71 private Spanner spanner(){72 SpannerOptions options = SpannerOptions73 .newBuilder()74 .setEmulatorHost(spannerEmulator.getEmulatorGrpcEndpoint())75 .setCredentials(NoCredentials.getInstance())76 .setProjectId(PROJECT_ID)77 .build();78 Spanner spanner = options.getService();79 return spanner;80 }81 @Autowired82 private OrderService orderService;83 @Before84 public static void setup() throws ExecutionException, InterruptedException {85 }86 @Test87 public void test() throws ExecutionException, InterruptedException {88 Spanner spanner = spanner();...
Source:AbstractIntegrationTest.java
...33 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 }49 void dropTables() {50 List<String> ddl = Stream51 .of(FileUtils.readFileAsString(DDL_DROPPING_FILE).split(QUERY_SEPARATOR))...
Source:SpannerEmulatorContainer.java
...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 }28 /**29 * @return a <code>host:port</code> pair corresponding to the address on which the emulator's30 * HTTP REST endpoint is reachable from the test host machine.31 */32 public String getEmulatorHttpEndpoint() {33 return getContainerIpAddress() + ":" + getMappedPort(HTTP_PORT);34 }35}...
getEmulatorGrpcEndpoint
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.testcontainers.containers.SpannerEmulatorContainer;6public class 1 {7 private static final String SPANNER_EMULATOR_HOST_ENV_VAR = "SPANNER_EMULATOR_HOST";8 private static final String SPANNER_EMULATOR_HOST = System.getenv(SPANNER_EMULATOR_HOST_ENV_VAR);9 private static final String SPANNER_PROJECT = "test-project";10 private static final String SPANNER_INSTANCE = "test-instance";11 private static final String SPANNER_DATABASE = "test-database";12 public static void main(String[] args) {13 SpannerEmulatorContainer emulatorContainer = new SpannerEmulatorContainer();14 emulatorContainer.start();15 String emulatorGrpcEndpoint = emulatorContainer.getEmulatorGrpcEndpoint();16 System.out.println("Spanner emulator gRPC endpoint: " + emulatorGrpcEndpoint);17 SpannerOptions spannerOptions = SpannerOptions.newBuilder()18 .setHost(emulatorGrpcEndpoint)19 .build();20 Spanner spanner = spannerOptions.getService();21 DatabaseId databaseId = DatabaseId.of(spannerOptions.getProjectId(), SPANNER_INSTANCE, SPANNER_DATABASE);22 DatabaseClient databaseClient = spanner.getDatabaseClient(da
getEmulatorGrpcEndpoint
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.SpannerException;5import com.google.cloud.spanner.SpannerOptions;6import org.testcontainers.containers.SpannerEmulatorContainer;7import org.testcontainers.utility.DockerImageName;8public class TestContainersDemo {9 public static void main(String[] args) {10 try (SpannerEmulatorContainer emulator = new SpannerEmulatorContainer(DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator:latest"))) {11 emulator.start();12 SpannerOptions options = SpannerOptions.newBuilder().setHost(emulator.getEmulatorGrpcEndpoint()).build();13 Spanner spanner = options.getService();14 DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("test-instance", "test-database"));15 client.readWriteTransaction().run(transaction -> {16 long rowCount = transaction.executeUpdate(com.google.cloud.spanner.Statement.of("INSERT INTO test_table (test_column) VALUES ('test')"));17 System.out.println("Inserted " + rowCount + " row(s)");18 return null;19 });20 spanner.close();21 } catch (SpannerException e) {22 e.printStackTrace();23 }24 }25}26Inserted 1 row(s)
getEmulatorGrpcEndpoint
Using AI Code Generation
1import com.google.cloud.spanner.DatabaseAdminClient;2import com.google.cloud.spanner.Spanner;3import com.google.cloud.spanner.SpannerOptions;4import org.junit.ClassRule;5import org.junit.Test;6import org.testcontainers.containers.SpannerEmulatorContainer;7public class TestcontainersExample {8 public static final SpannerEmulatorContainer spanner = new SpannerEmulatorContainer();9 public void test() {10 SpannerOptions options = SpannerOptions.newBuilder()11 .setHost(spanner.getEmulatorGrpcEndpoint())12 .setProjectId("test-project")13 .build();14 Spanner spanner = options.getService();15 DatabaseAdminClient dbClient = spanner.getDatabaseAdminClient();16 }17}
getEmulatorGrpcEndpoint
Using AI Code Generation
1import org.testcontainers.containers.SpannerEmulatorContainer;2public class 1 {3 public static void main(String[] args) {4 SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();5 spannerEmulatorContainer.start();6 String emulatorGrpcEndpoint = spannerEmulatorContainer.getEmulatorGrpcEndpoint();7 System.out.println(emulatorGrpcEndpoint);8 }9}
getEmulatorGrpcEndpoint
Using AI Code Generation
1import org.testcontainers.containers.SpannerEmulatorContainer;2public class Test {3 public static void main(String[] args) {4 SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();5 spannerEmulatorContainer.start();6 System.out.println(spannerEmulatorContainer.getEmulatorGrpcEndpoint());7 spannerEmulatorContainer.stop();8 }9}10import com.google.cloud.spanner.Database;11import com.google.cloud.spanner.DatabaseClient;12import com.google.cloud.spanner.DatabaseId;13import com.google.cloud.spanner.InstanceId;14import com.google.cloud.spanner.Spanner;15import com.google.cloud.spanner.SpannerOptions;16public class Test {17 public static void main(String[] args) {18 SpannerOptions options = SpannerOptions.newBuilder().setEmulatorHost("localhost:9010").build();19 Spanner spanner = options.getService();20 DatabaseId db = DatabaseId.of(InstanceId.of("test-instance"), "test-db");21 DatabaseClient dbClient = spanner.getDatabaseClient(db);22 .createDatabase("test-db", 23 Arrays.asList(24 "CREATE TABLE test_table (id INT64, name STRING(MAX)) PRIMARY KEY (id)"25 )).get();26 System.out.println("Created database [" + database.getId() + "]");27 }28}
getEmulatorGrpcEndpoint
Using AI Code Generation
1package org.testcontainers.containers;2import com.google.cloud.spanner.Spanner;3import com.google.cloud.spanner.SpannerOptions;4import org.junit.Test;5public class SpannerEmulatorContainerTest {6 public void testEmulator() {7 SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();8 spannerEmulatorContainer.start();9 SpannerOptions options = SpannerOptions.newBuilder()10 .setProjectId("test-project")11 .setEmulatorHost(spannerEmulatorContainer.getEmulatorGrpcEndpoint())12 .build();13 Spanner spanner = options.getService();14 spanner.getDatabaseAdminClient().listDatabases("test-project");15 }16}17package org.testcontainers.containers;18import com.google.cloud.spanner.Spanner;19import com.google.cloud.spanner.SpannerOptions;20import org.junit.Test;21public class SpannerEmulatorContainerTest {22 public void testEmulator() {23 SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();24 spannerEmulatorContainer.start();25 SpannerOptions options = SpannerOptions.newBuilder()26 .setProjectId("test-project")27 .setEmulatorHost(spannerEmulatorContainer.getEmulatorRestEndpoint())28 .build();29 Spanner spanner = options.getService();30 spanner.getDatabaseAdminClient().listDatabases("test-project");31 }32}33testEmulator(org.testcontainers.containers.SpannerEmulatorContainerTest) Time elapsed: 0.175 sec <<< ERROR!34 at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)35 at com.google.cloud.spanner.SpannerOptions$Builder.setEmulatorHost(SpannerOptions.java:385)36 at org.testcontainers.containers.SpannerEmulatorContainerTest.testEmulator(SpannerEmulatorContainerTest.java:20)37 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)38 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)39 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
getEmulatorGrpcEndpoint
Using AI Code Generation
1SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();2emulator.start();3SpannerOptions options = SpannerOptions.newBuilder().setEmulatorHost(emulator.getEmulatorGrpcEndpoint()).build();4Spanner spanner = options.getService();5DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of("test-project", "test-instance", "test-database"));6SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();7emulator.start();8SpannerOptions options = SpannerOptions.newBuilder().setEmulatorHost(emulator.getEmulatorGrpcEndpoint()).build();9Spanner spanner = options.getService();10DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of("test-project", "test-instance", "test-database"));11SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();12emulator.start();13SpannerOptions options = SpannerOptions.newBuilder().setEmulatorHost(emulator.getEmulatorGrpcEndpoint()).build();14Spanner spanner = options.getService();15DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of("test-project", "test-instance", "test-database"));16SpannerEmulatorContainer emulator = new SpannerEmulatorContainer();17emulator.start();18SpannerOptions options = SpannerOptions.newBuilder().setEmulatorHost(emulator.getEmulatorGrpcEndpoint()).build();19Spanner spanner = options.getService();20DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of("test-project", "test-instance", "test-database
getEmulatorGrpcEndpoint
Using AI Code Generation
1package org.testcontainers.containers;2import com.google.cloud.spanner.DatabaseClient;3import com.google.cloud.spanner.DatabaseId;4import com.google.cloud.spanner.DatabaseOptions;5import com.google.cloud.spanner.Database;6import com.google.cloud.spanner.InstanceId;7import com.google.cloud.spanner.Spanner;8import com.google.cloud.spanner.SpannerOptions;9import com.google.cloud.spanner.Statement;10import com.google.cloud.spanner.ResultSet;11import com.google.cloud.spanner.Mutation;12import com.google.cloud.spanner.SpannerException;13import com.google.cloud.spanner.SpannerExceptionFactory;14import java.util.Arrays;15import java.util.ArrayList;16public class SpannerEmulatorContainerTest {17 public static void main(String[] args) {18 try (SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()) {19 emulator.start();20 final String emulatorHost = emulator.getEmulatorGrpcEndpoint();21 System.out.println("emulatorHost: " + emulatorHost);22 final SpannerOptions options = SpannerOptions.newBuilder()23 .setHost(emulatorHost)24 .build();25 final Spanner spanner = options.getService();26 final DatabaseId db = DatabaseId.of(InstanceId.of(options.getProjectId(), "emulator"), "test-db");27 final DatabaseClient client = spanner.getDatabaseClient(db);28 final Database database = client.getDatabaseClient().createDatabase(db.getInstanceId().getInstance(), db.getDatabase(), Arrays.asList("CREATE TABLE Users (UserId INT64 NOT NULL, Name STRING(100), Email STRING(100)) PRIMARY KEY (UserId)"));29 final Mutation mutation = Mutation.newInsertBuilder("Users")30 .set("UserId").to(1L)31 .set("Name").to("John Doe")32 .set("Email").to("
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!!