Home > OS >  Integration testing with Testcontainers/Localstack and Spring Boot: Set directory for temporary file
Integration testing with Testcontainers/Localstack and Spring Boot: Set directory for temporary file

Time:10-06

I am using Testcontainers' (Localstack Module)[https://www.testcontainers.org/modules/localstack/] for advanced integration testing with Spring Boot, replacing the AmazonS3 client with the one from Localstack. I have set up my integration test roughly as follows (only relevant parts):

// FurnitureDetailsControllerIT.kt

@Testcontainers
@SpringBootTest
@ExtendWith(SpringExtension::class)
@AutoConfigureMockMvc
@DirtiesContext
class FurnitureDetailsControllerIT {

  @Autowired
  private val amazonS3: AmazonS3? = null

  
  companion object {
    @Container
    var localStack: LocalStackContainer =
      LocalStackContainer(DockerImageName.parse("localstack/localstack:latest")).withServices(LocalStackContainer.Service.S3)

    @JvmStatic
    @DynamicPropertySource
    fun properties(registry: DynamicPropertyRegistry) {
      registry.add("cloud.aws.s3.endpoint") { localStack.getEndpointOverride(LocalStackContainer.Service.S3) }
      registry.add("cloud.aws.credentials.access-key") { localStack.accessKey }
      registry.add("cloud.aws.credentials.secret-key") { localStack.secretKey }
    }
  }
(...)
}

Everything runs fine when running the integration test locally (from my IDE or command line using Maven), meaning I can see the localstack container spinning up in the logs:

13:54:16.360 [main] INFO            
  • Related