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

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

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

问题

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

  1. @SpringBootTest
  2. @Testcontainers
  3. public class KafkaContainerTest {
  4. @ClassRule
  5. public static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.2.1"));
  6. @Test
  7. public void testUsage() throws Exception {
  8. kafka.start();
  9. testKafkaFunctionality(kafka.getBootstrapServers());
  10. }
  11. //...
  12. }

build.gradle

  1. plugins {
  2. id 'org.springframework.boot' version '2.3.4.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.example'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '1.8'
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation 'org.springframework.boot:spring-boot-starter'
  14. implementation 'org.springframework.boot:spring-boot-starter-web'
  15. implementation 'org.springframework.boot:spring-boot-starter-webflux'
  16. compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.3.4.RELEASE'
  17. implementation 'org.apache.kafka:kafka-streams'
  18. implementation 'org.springframework.kafka:spring-kafka'
  19. compileOnly 'org.projectlombok:lombok'
  20. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  21. annotationProcessor 'org.projectlombok:lombok'
  22. testCompile 'org.projectlombok:lombok'
  23. testCompile group: 'io.projectreactor', name: 'reactor-test', version: '3.3.10.RELEASE'
  24. testImplementation 'io.projectreactor:reactor-test'
  25. testImplementation 'org.springframework.kafka:spring-kafka-test'
  26. testImplementation 'com.google.guava:guava:23.0'
  27. testImplementation 'org.testcontainers:testcontainers:1.14.3'
  28. testImplementation "org.testcontainers:junit-jupiter:1.14.3"
  29. testImplementation 'org.testcontainers:kafka:1.14.3'
  30. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  31. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  32. }
  33. }
  34. test {
  35. useJUnitPlatform()
  36. }

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

更新

以下内容有效:

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

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

  1. @SpringBootTest
  2. @Testcontainers
  3. public class KafkaContainerTest {
  4. @ClassRule
  5. public static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.2.1"));
  6. @Test
  7. public void testUsage() throws Exception {
  8. kafka.start();
  9. testKafkaFunctionality(kafka.getBootstrapServers());
  10. }
  11. //...
  12. }

build.gradle

  1. plugins {
  2. id 'org.springframework.boot' version '2.3.4.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.example'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '1.8'
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation 'org.springframework.boot:spring-boot-starter'
  14. implementation 'org.springframework.boot:spring-boot-starter-web'
  15. implementation 'org.springframework.boot:spring-boot-starter-webflux'
  16. compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.3.4.RELEASE'
  17. implementation 'org.apache.kafka:kafka-streams'
  18. implementation 'org.springframework.kafka:spring-kafka'
  19. compileOnly 'org.projectlombok:lombok'
  20. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  21. annotationProcessor 'org.projectlombok:lombok'
  22. testCompile 'org.projectlombok:lombok'
  23. testCompile group: 'io.projectreactor', name: 'reactor-test', version: '3.3.10.RELEASE'
  24. testImplementation 'io.projectreactor:reactor-test'
  25. testImplementation 'org.springframework.kafka:spring-kafka-test'
  26. testImplementation 'com.google.guava:guava:23.0'
  27. testImplementation 'org.testcontainers:testcontainers:1.14.3'
  28. testImplementation "org.testcontainers:junit-jupiter:1.14.3"
  29. testImplementation 'org.testcontainers:kafka:1.14.3'
  30. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  31. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  32. }
  33. }
  34. test {
  35. useJUnitPlatform()
  36. }

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

  1. testImplementation 'org.testcontainers:testcontainers:1.15.0-rc2'
  2. testImplementation "org.testcontainers:junit-jupiter:1.15.0-rc2"
  3. 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 对象的构造函数:

  1. 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:

  1. 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:

确定