Cannot resolve method ‘parse’ in ‘DockerImageName’ when using Testcontainers and gradle in JUnit5

huangapple go评论105阅读模式
英文:

Cannot resolve method 'parse' in 'DockerImageName' when using Testcontainers and gradle in JUnit5

问题

错误消息为:无法解析方法 'parse' 在 'DockerImageName' 中

@SpringBootTest
@Testcontainers
public class KafkaContainerTest {
    @ClassRule
    public static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.2.1"));

    @Test
    public void testUsage() throws Exception {
        kafka.start();
        testKafkaFunctionality(kafka.getBootstrapServers());
    }
    //...
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.3.4.RELEASE'
    implementation 'org.apache.kafka:kafka-streams'
    implementation 'org.springframework.kafka:spring-kafka'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testCompile 'org.projectlombok:lombok'
    testCompile group: 'io.projectreactor', name: 'reactor-test', version: '3.3.10.RELEASE'
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.springframework.kafka:spring-kafka-test'
    testImplementation 'com.google.guava:guava:23.0'
    testImplementation 'org.testcontainers:testcontainers:1.14.3'
    testImplementation "org.testcontainers:junit-jupiter:1.14.3"
    testImplementation 'org.testcontainers:kafka:1.14.3'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

当在 IntelliJ 中导航到 DockerImageName 类时,显示 Library source does not match the bytecode for class DockerImageName这个答案 没有帮助。

更新

以下内容有效:

testImplementation 'org.testcontainers:testcontainers:1.15.0-rc2'
testImplementation "org.testcontainers:junit-jupiter:1.15.0-rc2"
testImplementation 'org.testcontainers:kafka:1.15.0-rc2'
英文:

The test fails to compile with error Cannot resolve method 'parse' in 'DockerImageName'

@SpringBootTest
@Testcontainers
public class KafkaContainerTest {
    @ClassRule
    public static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.2.1"));

    @Test
    public void testUsage() throws Exception {
            kafka.start();
            testKafkaFunctionality(kafka.getBootstrapServers());
    }
//...
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.3.4.RELEASE'
    implementation 'org.apache.kafka:kafka-streams'
    implementation 'org.springframework.kafka:spring-kafka'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testCompile 'org.projectlombok:lombok'
    testCompile group: 'io.projectreactor', name: 'reactor-test', version: '3.3.10.RELEASE'
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.springframework.kafka:spring-kafka-test'
    testImplementation 'com.google.guava:guava:23.0'
    testImplementation 'org.testcontainers:testcontainers:1.14.3'
    testImplementation "org.testcontainers:junit-jupiter:1.14.3"
    testImplementation 'org.testcontainers:kafka:1.14.3'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

When navigating to DockerImageName class in IntelliJ it says Library source does not match the bytecode for class DockerImageName. This answer did not help.

Update

This works

testImplementation 'org.testcontainers:testcontainers:1.15.0-rc2'
testImplementation "org.testcontainers:junit-jupiter:1.15.0-rc2"
testImplementation 'org.testcontainers:kafka:1.15.0-rc2'

答案1

得分: 4

静态的 parse 方法是在今年7月才添加到 testcontainers 库中的。你正在使用的是版本 1.14.3,该版本是在5月份发布的。我预计如果你将库的版本升级到 15.X(版本 1.15.0-rc2 似乎是最新的版本),你的问题将会得到解决。

或者,你可以将你对该库的使用方式更改为版本 1.14.3 中所支持的方式。

更新:我刚刚看到(并且终于理解了)@tgdavies 的建议,你可以直接通过以下方式调用 DockerImageName 对象的构造函数:

return new DockerImageName(fullImageName);
英文:

The static parse method was only added to the testcontainers library back in July. You're using version 1.14.3, which was released in May. I expect that if you upgrade the version of the library to a 15.X (version 1.15.0-rc2 seems to be the latest), I expect your problem will be resolved.

Alternately, you can change your usage of that library to what it was capable of in version 1.14.3.

UPDATE: I just saw (and finally understood) @tgdavies's suggestion that you can just call the DockerImageName object's constructor directly via:

return new DockerImageName(fullImageName);

huangapple
  • 本文由 发表于 2020年10月18日 04:26:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64407003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定