79487159

Date: 2025-03-05 16:57:25
Score: 0.5
Natty:
Report link

I wasn't aware of this concept yet. I heard of it, but not knowledgeable enough that it would help me now. Test containers.

import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import net.bytebuddy.utility.RandomString;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.FirestoreEmulatorContainer;
import org.testcontainers.utility.DockerImageName;


@TestConfiguration
public class DockerEmulator {

    private static FirestoreEmulatorContainer firestoreEmulatorContainer = new FirestoreEmulatorContainer(
            DockerImageName.parse("gcr.io/google.com/cloudsdktool/google-cloud-cli:488.0.0-emulators")
    );

    @Bean
    public Firestore firestore() {
        firestoreEmulatorContainer.start();
        FirestoreOptions options = FirestoreOptions
                .getDefaultInstance()
                .toBuilder()
                .setProjectId(RandomString.make().toLowerCase())
                .setCredentials(NoCredentials.getInstance())
                .setHost(firestoreEmulatorContainer.getEmulatorEndpoint())
                .build();

        System.out.println(options.getApplicationName());
        return options.getService();
    }
}

Straight from Baeldung blog.

Include that in the context of my test

@SpringJUnitConfig(classes = { SpringTestConfiguration.class, DockerEmulator.class })
@ActiveProfiles("test")
class FutonMigrationOnStartUpTest
Reasons:
  • Blacklisted phrase (1): help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Thomas E