0

I want to use test container with spock on my spring boot application.

these are my dependencies :

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-redis')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile 'org.testcontainers:spock:1.8.3'
    runtime('org.springframework.boot:spring-boot-devtools')
    compile 'org.codehaus.groovy:groovy-all:2.4.15'
    compileOnly('org.projectlombok:lombok')

    compile 'org.testcontainers:testcontainers:1.8.3'
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile "org.spockframework:spock-core:1.1-groovy-2.4-rc-4"
    testCompile "org.spockframework:spock-spring:1.1-groovy-2.4-rc-4"
    testCompile 'com.github.testcontainers:testcontainers-spock:-SNAPSHOT'
}

I have initialized my test like below :

@SpringBootTest
@Testcontainers
class ProductRedisRepositoryTest extends Specification {

    @Autowired
    ProductRedisRepository productRedisRepository

    @Autowired
    TestComponent testComponent


    static Consumer<CreateContainerCmd> cmd = { -> e.withPortBindings(new PortBinding(Ports.Binding.bindPort(6379), new ExposedPort(6379)))}

    @Shared
    public static GenericContainer redis =
            new GenericContainer("redis:3.0.2")
                    //.withExposedPorts(6379)
                    .withCreateContainerCmdModifier(cmd)

    def "check redis repository save and get"(){

        given:
            Product product = Product.builder()
                    .brand("brand")
                    .id("id")
                    .model("model")
                    .name( "name")
                    .build()
        when:
            productRedisRepository.save(product)
            Product persistProduct = productRedisRepository.find("id")

        then:
            persistProduct.getName() == product.getName()
    }

}

But it doesn't initiate redis container when I run test. What is my mistake. How can I do that.

My springBootVersion = '2.0.4.RELEASE' and I am using Intelij.

This is the log output : LOG

sam
  • 1,073
  • 4
  • 13
  • 27
  • can you provide log output? – gesellix Aug 21 '18 at 10:27
  • Please consider changing `compile 'org.testcontainers:spock...'` to `testCompile 'org.testcontainers:spock...'`. You can also remove the obsolete `testCompile 'com.github.testcontainers:testcontainers-spock...'` line. – gesellix Aug 21 '18 at 10:29
  • @gesellix I did what you say but the result still same. I have added log output at the end of the post. – sam Aug 21 '18 at 12:00
  • another typo: please change `{ -> e.withPortBindings ...` to `{ e -> e.withPortBindings ...`. Or even simpler with `{ it.withPortBindings ...`. – gesellix Aug 21 '18 at 13:24

1 Answers1

4

Please remove the static keyword at your @Shared GenericContainer field.

Static fields won't be annotated with @FieldMetadata by the Spock Framework, hence they won't be considered as part of the spec's fields. Testcontainers-Spock relies on those fields to recognise GenericContainers.

If you need the static modifier though, you can work around the issue like this:

...

public static GenericContainer staticRedis =
        new GenericContainer("redis:3.0.2")
                //.withExposedPorts(6379)
                .withCreateContainerCmdModifier(cmd)

@Shared
public GenericContainer redis = staticRedis

...
gesellix
  • 3,024
  • 28
  • 31