-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
When I use @EmbeddedKafka with JUnit5 and make use of the KafkaTestUtils, I have gotten the following exception during ./gradlew build command being executed:
e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class org.springframework.kafka.test.rule.EmbeddedKafkaRule, unresolved supertypes: org.junit.rules.ExternalResource, org.junit.rules.TestRule
class org.springframework.kafka.test.rule.KafkaRule, unresolved supertypes: org.junit.rules.TestRule
> Task :compileTestKotlin FAILED
FAILURE: Build failed with an exception.
After the investigation I figured out where the problem is:
I was using KafkaTestUtils#consumerProps to create properties of the consumer, however, the file KafkaTestUtils uses indirectly org.springframework.kafka.test.rule.EmbeddedKafkaRule, which is not supposed to be used together with JUnit5.
There are two methods: public static Map<String, Object> consumerProps(String group, String autoCommit, KafkaEmbedded embeddedKafka) and public static Map<String, Object> producerProps(KafkaEmbedded embeddedKafka), which both are marked as deprecated, and this two methods have EmbeddedKafkaRule as their argument, which is dependent on JUnit4 resources and this is why it did not work without dependencies of JUnit4
Would it be possible to have two separated utils files? One specifically for JUnit4 and another one for JUnit5?
My gradle file is:
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.kafka:spring-kafka")
implementation("org.simpleflatmapper:sfm-csv:$csvFormatter")
implementation("com.amazonaws:aws-java-sdk:$awsJavaSdk")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude("junit", "junit")
}
testImplementation("cloud.localstack:localstack-utils:$localStackVersion")
testImplementation("org.springframework.kafka:spring-kafka-test")
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:$junitVersion")
My test is:
@ExtendWith(value = [SpringExtension::class])
@SpringBootTest
@EmbeddedKafka
@DirtiesContext
@TestInstance(PER_CLASS)
@DisplayName("Kafka and S3 integration tests")
class KafkaSaverTestsGitHub {
@Suppress("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
lateinit var kafkaBroker: EmbeddedKafkaBroker
@Test
fun shouldNotBeNull() {
assertThat(kafkaBroker).isNotNull
}
private fun getConsumerConfig(): HashMap<String, Any> {
val configs = HashMap(KafkaTestUtils.consumerProps("consumer", "false", kafkaBroker))
configs[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
return configs
}