1

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'
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
  • What is `DockerImageName`? Whatever it is, it apparently doesn't have a `parse()` method, and so your code is syntactically incorrect because you're trying to call such a method on that object. – CryptoFool Oct 17 '20 at 20:32
  • It is `import org.testcontainers.utility.DockerImageName;` as suggested in https://www.testcontainers.org/modules/kafka/ and https://github.com/testcontainers/testcontainers-java/blob/1e597ccd4fa1a7113d3077599e0b13237c1fef51/core/src/main/java/org/testcontainers/utility/DockerImageName.java – J.Olufsen Oct 17 '20 at 20:39
  • I concur that you are making a correct call there. It must be that that class isn't defined, or is defined as something else than what you're showing. What does the `import` statement look like for that class? You do have one for it at the top of the file, I hope. – CryptoFool Oct 17 '20 at 20:48
  • That method was added recently https://github.com/testcontainers/testcontainers-java/commit/c3f53b3a63e6b0bc800a7f0fbce91ce95a8986b3#diff-44a23d51c0686477fbe698e7066e4f140355e4fdbc5ae973565fe89bf000f53b I think you should try 1.15.0-rc2 – tgdavies Oct 17 '20 at 20:51
  • or why not `new DockerImageName(...)`? – tgdavies Oct 17 '20 at 20:51
  • @tgdavies - I think you might have something there. Usually, when the source and object are out of sync, it is the object that is older. At least that's the way it usually seems to be for me. – CryptoFool Oct 17 '20 at 20:52
  • 1
    If you look at the source, all it does is `public static DockerImageName parse(String fullImageName) { return new DockerImageName(fullImageName); } `(of course that might change in the future, so calling the static method is preferred, but just creating an instance is an acceptable workaround if you don't want to go to the rc.) – tgdavies Oct 17 '20 at 21:01
  • 1
    @tgdavies - didn't want to steal points from you or anything...just wanted this question to have a real answer. I came around to the answer myself, but you were out in front of me. If you want the credit for this, just copy my answer and paste it as a new one, or create one of your own, and I'll delete mine. – CryptoFool Oct 17 '20 at 21:06
  • No worries Steve – tgdavies Oct 17 '20 at 21:09

1 Answers1

4

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);
CryptoFool
  • 21,719
  • 5
  • 26
  • 44