From ea606665139a8c7ff3ba4a85e0e99314403729fd Mon Sep 17 00:00:00 2001 From: Ken Huang <100591800+m1a2st@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:11:37 +0900 Subject: [PATCH] KAFKA-16921 [1/N] Migrate all junit 4 code to junit 5 for connect module (#16253) Reviewers: Chia-Ping Tsai --- build.gradle | 63 ++++++++----------- .../connect/cli/ConnectStandaloneTest.java | 10 +-- ...nnectorClientConfigOverridePolicyTest.java | 7 ++- ...nnectorClientConfigOverridePolicyTest.java | 2 +- ...nnectorClientConfigOverridePolicyTest.java | 2 +- .../converters/BooleanConverterTest.java | 18 +++--- .../converters/ByteArrayConverterTest.java | 14 ++--- .../converters/NumberConverterTest.java | 14 ++--- 8 files changed, 60 insertions(+), 70 deletions(-) diff --git a/build.gradle b/build.gradle index 28ec61b5bfd..b1970faa188 100644 --- a/build.gradle +++ b/build.gradle @@ -427,9 +427,6 @@ subprojects { } } - // Remove the relevant project name once it's converted to JUnit 5 - def shouldUseJUnit5 = !(["runtime"].contains(it.project.name)) - def testLoggingEvents = ["passed", "skipped", "failed"] def testShowStandardStreams = false def testExceptionFormat = 'full' @@ -522,8 +519,7 @@ subprojects { exclude testsToExclude - if (shouldUseJUnit5) - useJUnitPlatform() + useJUnitPlatform() retry { maxRetries = userMaxTestRetries @@ -550,23 +546,18 @@ subprojects { exclude testsToExclude - if (shouldUseJUnit5) { - if (project.name == 'streams') { - useJUnitPlatform { - includeTags "integration" - includeTags "org.apache.kafka.test.IntegrationTest" - // Both engines are needed to run JUnit 4 tests alongside JUnit 5 tests. - // junit-vintage (JUnit 4) can be removed once the JUnit 4 migration is complete. - includeEngines "junit-vintage", "junit-jupiter" - } - } else { - useJUnitPlatform { - includeTags "integration" - } + if (project.name == 'streams') { + useJUnitPlatform { + includeTags "integration" + includeTags "org.apache.kafka.test.IntegrationTest" + // Both engines are needed to run JUnit 4 tests alongside JUnit 5 tests. + // junit-vintage (JUnit 4) can be removed once the JUnit 4 migration is complete. + includeEngines "junit-vintage", "junit-jupiter" } } else { - useJUnit { - includeCategories 'org.apache.kafka.test.IntegrationTest' + useJUnitPlatform { + includeTags "integration" + includeTags 'org.apache.kafka.test.IntegrationTest' } } @@ -593,23 +584,18 @@ subprojects { exclude testsToExclude - if (shouldUseJUnit5) { - if (project.name == 'streams') { - useJUnitPlatform { - excludeTags "integration" - excludeTags "org.apache.kafka.test.IntegrationTest" - // Both engines are needed to run JUnit 4 tests alongside JUnit 5 tests. - // junit-vintage (JUnit 4) can be removed once the JUnit 4 migration is complete. - includeEngines "junit-vintage", "junit-jupiter" - } - } else { - useJUnitPlatform { - excludeTags "integration" - } + if (project.name == 'streams') { + useJUnitPlatform { + excludeTags "integration" + excludeTags "org.apache.kafka.test.IntegrationTest" + // Both engines are needed to run JUnit 4 tests alongside JUnit 5 tests. + // junit-vintage (JUnit 4) can be removed once the JUnit 4 migration is complete. + includeEngines "junit-vintage", "junit-jupiter" } } else { - useJUnit { - excludeCategories 'org.apache.kafka.test.IntegrationTest' + useJUnitPlatform { + excludeTags "integration" + excludeTags 'org.apache.kafka.test.IntegrationTest' } } @@ -3229,10 +3215,13 @@ project(':connect:runtime') { testImplementation project(':storage') testImplementation project(':connect:test-plugins') testImplementation project(':group-coordinator') - - testImplementation libs.junitJupiterApi + + testImplementation libs.junitJupiter testImplementation libs.junitVintageEngine + testImplementation libs.mockitoJunitJupiter testImplementation libs.mockitoCore + testImplementation libs.hamcrest + testImplementation libs.mockitoJunitJupiter testImplementation libs.httpclient testRuntimeOnly libs.slf4jlog4j diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/cli/ConnectStandaloneTest.java b/connect/runtime/src/test/java/org/apache/kafka/connect/cli/ConnectStandaloneTest.java index 9a762f1a394..2821bf43155 100644 --- a/connect/runtime/src/test/java/org/apache/kafka/connect/cli/ConnectStandaloneTest.java +++ b/connect/runtime/src/test/java/org/apache/kafka/connect/cli/ConnectStandaloneTest.java @@ -19,8 +19,8 @@ package org.apache.kafka.connect.cli; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.kafka.connect.runtime.rest.entities.CreateConnectorRequest; import org.apache.kafka.test.TestUtils; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.File; import java.io.FileWriter; @@ -30,8 +30,8 @@ import java.util.Map; import java.util.Properties; import static org.apache.kafka.connect.runtime.ConnectorConfig.NAME_CONFIG; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; public class ConnectStandaloneTest { @@ -46,7 +46,7 @@ public class ConnectStandaloneTest { private final ConnectStandalone connectStandalone = new ConnectStandalone(); private File connectorConfigurationFile; - @Before + @BeforeEach public void setUp() throws IOException { connectorConfigurationFile = TestUtils.tempFile(); } diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/BaseConnectorClientConfigOverridePolicyTest.java b/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/BaseConnectorClientConfigOverridePolicyTest.java index b6a73d56de9..1845cf09081 100644 --- a/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/BaseConnectorClientConfigOverridePolicyTest.java +++ b/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/BaseConnectorClientConfigOverridePolicyTest.java @@ -20,11 +20,12 @@ package org.apache.kafka.connect.connector.policy; import org.apache.kafka.common.config.ConfigValue; import org.apache.kafka.connect.health.ConnectorType; import org.apache.kafka.connect.runtime.SampleSourceConnector; -import org.junit.Assert; import java.util.List; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertTrue; + public abstract class BaseConnectorClientConfigOverridePolicyTest { protected abstract ConnectorClientConfigOverridePolicy policyToTest(); @@ -50,10 +51,10 @@ public abstract class BaseConnectorClientConfigOverridePolicyTest { } protected void assertNoError(List configValues) { - Assert.assertTrue(configValues.stream().allMatch(configValue -> configValue.errorMessages().isEmpty())); + assertTrue(configValues.stream().allMatch(configValue -> configValue.errorMessages().isEmpty())); } protected void assertError(List configValues) { - Assert.assertTrue(configValues.stream().anyMatch(configValue -> !configValue.errorMessages().isEmpty())); + assertTrue(configValues.stream().anyMatch(configValue -> !configValue.errorMessages().isEmpty())); } } diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/NoneConnectorClientConfigOverridePolicyTest.java b/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/NoneConnectorClientConfigOverridePolicyTest.java index 2c7b0789d80..63d2a774cb2 100644 --- a/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/NoneConnectorClientConfigOverridePolicyTest.java +++ b/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/NoneConnectorClientConfigOverridePolicyTest.java @@ -19,7 +19,7 @@ package org.apache.kafka.connect.connector.policy; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.config.SaslConfigs; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.HashMap; diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/PrincipalConnectorClientConfigOverridePolicyTest.java b/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/PrincipalConnectorClientConfigOverridePolicyTest.java index 0e79c8a6ca9..03d4d2c940d 100644 --- a/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/PrincipalConnectorClientConfigOverridePolicyTest.java +++ b/connect/runtime/src/test/java/org/apache/kafka/connect/connector/policy/PrincipalConnectorClientConfigOverridePolicyTest.java @@ -19,7 +19,7 @@ package org.apache.kafka.connect.connector.policy; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.config.SaslConfigs; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.HashMap; diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/converters/BooleanConverterTest.java b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/BooleanConverterTest.java index d7f9654130c..fbdda211e4e 100644 --- a/connect/runtime/src/test/java/org/apache/kafka/connect/converters/BooleanConverterTest.java +++ b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/BooleanConverterTest.java @@ -17,12 +17,12 @@ package org.apache.kafka.connect.converters; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.charset.StandardCharsets; import java.util.Collections; @@ -30,8 +30,8 @@ import java.util.Collections; import org.apache.kafka.common.utils.AppInfoParser; import org.apache.kafka.connect.data.Schema; import org.apache.kafka.connect.errors.DataException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class BooleanConverterTest { private static final String TOPIC = "topic"; @@ -39,7 +39,7 @@ public class BooleanConverterTest { private static final byte[] FALSE = new byte[] {0x00}; private final BooleanConverter converter = new BooleanConverter(); - @Before + @BeforeEach public void setUp() { converter.configure(Collections.emptyMap(), false); } diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java index 748fd70e63f..b2c572133f7 100644 --- a/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java +++ b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java @@ -21,17 +21,17 @@ import org.apache.kafka.common.utils.AppInfoParser; import org.apache.kafka.connect.data.Schema; import org.apache.kafka.connect.data.SchemaAndValue; import org.apache.kafka.connect.errors.DataException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Collections; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public class ByteArrayConverterTest { private static final String TOPIC = "topic"; @@ -39,7 +39,7 @@ public class ByteArrayConverterTest { private final ByteArrayConverter converter = new ByteArrayConverter(); - @Before + @BeforeEach public void setUp() { converter.configure(Collections.emptyMap(), false); } diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/converters/NumberConverterTest.java b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/NumberConverterTest.java index f57fd73d458..868ef47bafc 100644 --- a/connect/runtime/src/test/java/org/apache/kafka/connect/converters/NumberConverterTest.java +++ b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/NumberConverterTest.java @@ -21,13 +21,13 @@ import org.apache.kafka.common.utils.AppInfoParser; import org.apache.kafka.connect.data.Schema; import org.apache.kafka.connect.data.SchemaAndValue; import org.apache.kafka.connect.errors.DataException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public abstract class NumberConverterTest { private static final String TOPIC = "topic"; @@ -46,7 +46,7 @@ public abstract class NumberConverterTest { protected abstract Schema schema(); - @Before + @BeforeEach public void setup() { converter = createConverter(); serializer = createSerializer();