我想在我的 Spring Boot 应用程序上使用带有 spock 的测试容器。
这些是我的依赖项:
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'
}
我已经初始化了我的测试,如下所示:
@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()
}
}
但是当我运行测试时它不会启动redis容器。我的错误是什么。我怎样才能做到这一点。
我的 springBootVersion = '2.0.4.RELEASE' 我正在使用 Intelij。
这是日志输出:LOG