1

I have problem with creating external configuration for my test classes. Right now my redis related tests have to have a companion object inside

@Testcontainers
@TestPropertySource("classpath:/application-test.properties")
@SpringBootTest
class RedisRelatedTest {

  companion object {
    @Container
    val container = GenericContainer<Nothing>("redis:5.0.7-alpine")
            .apply { withExposedPorts(6379) }

    @JvmStatic
    @DynamicPropertySource
    fun properties(registry: DynamicPropertyRegistry) {
        registry.add("spring.redis.host", container::getHost);
        registry.add("spring.redis.port", container::getFirstMappedPort);
    }
  }
  ... some tesitng
}

I'd like to move it somewhere outside and and use some one liner to include it but I can't find a way that works. I've created a RedisConfig class with its companion object but @Import(RedisConfig::class) or @Import(RedisConfig.Congig::class) is completely ignored.

Daniel Jacob
  • 1,455
  • 9
  • 17
ppysz
  • 380
  • 5
  • 21

2 Answers2

0

@Import(RedisConfig::class) doesn't do anything by itself, you still have to autowire a bean, did you do that as well?

One other option is to have an abstract class with a container and extend it in test classes.

Vitaly Chura
  • 704
  • 8
  • 13
0

@Import(RedisConfig::class) won't work. If it is @SpringTest you should rather use:

@SpringBootTest(
    classes = [RedisConfig::class])

but I'm not sure if testcontainer annotations will work as you expect. I solved this problem the way I described in this response to similar issue: https://stackoverflow.com/a/66532851/3484423