Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-10447: Migrate tools module to JUnit 5 and mockito #9231

Merged
merged 1 commit into from Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 55 additions & 25 deletions build.gradle
Expand Up @@ -241,6 +241,7 @@ subprojects {
}
}

def shouldUseJUnit5 = it.project.name == 'tools'
def testLoggingEvents = ["passed", "skipped", "failed"]
def testShowStandardStreams = false
def testExceptionFormat = 'full'
Expand Down Expand Up @@ -330,6 +331,9 @@ subprojects {
// Gradle will run each test class, so we exclude the suites to avoid redundantly running the tests twice.
exclude '**/*Suite.class'

if (shouldUseJUnit5)
useJUnitPlatform()

retry {
maxRetries = userMaxTestRetries
maxFailures = userMaxTestRetryFailures
Expand All @@ -355,8 +359,14 @@ subprojects {
// Gradle will run each test class, so we exclude the suites to avoid redundantly running the tests twice.
exclude '**/*Suite.class'

useJUnit {
includeCategories 'org.apache.kafka.test.IntegrationTest'
if (shouldUseJUnit5) {
useJUnitPlatform {
includeTags "integration"
}
} else {
useJUnit {
includeCategories 'org.apache.kafka.test.IntegrationTest'
}
}

retry {
Expand All @@ -383,9 +393,15 @@ subprojects {
// Gradle will run each test class, so we exclude the suites to avoid redundantly running the tests twice.
exclude '**/*Suite.class'

if (it.project.name != 'generator') {
useJUnit {
excludeCategories 'org.apache.kafka.test.IntegrationTest'
if (shouldUseJUnit5) {
useJUnitPlatform {
excludeTags "integration"
}
} else {
if (it.project.name != 'generator') {
useJUnit {
excludeCategories 'org.apache.kafka.test.IntegrationTest'
}
}
}

Expand Down Expand Up @@ -761,7 +777,8 @@ project(':core') {
testCompile libs.apachedsLdifPartition
testCompile libs.apachedsMavibotPartition
testCompile libs.apachedsJdbmPartition
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.scalatest
testCompile libs.slf4jlog4j
testCompile libs.jfreechart
Expand Down Expand Up @@ -982,7 +999,8 @@ project(':generator') {
compile libs.jacksonDatabind
compile libs.jacksonJDK8Datatypes
compile libs.jacksonJaxrsJsonProvider
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
}

integrationTest {
Expand Down Expand Up @@ -1016,7 +1034,8 @@ project(':clients') {
jacksonDatabindConfig libs.jacksonDatabind // to publish as provided scope dependency.

testCompile libs.bcpkix
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.mockitoCore

testRuntime libs.slf4jlog4j
Expand Down Expand Up @@ -1141,11 +1160,9 @@ project(':tools') {
compile libs.jettyServlets

testCompile project(':clients')
testCompile libs.junit
testCompile libs.junitJupiter
testCompile project(':clients').sourceSets.test.output
testCompile libs.easymock
testCompile libs.powermockJunit4
testCompile libs.powermockEasymock
testCompile libs.mockitoInline // supports mocking static methods, final classes, etc.

testRuntime libs.slf4jlog4j
}
Expand Down Expand Up @@ -1193,7 +1210,8 @@ project(':streams') {
testCompile project(':core')
testCompile project(':core').sourceSets.test.output
testCompile libs.log4j
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.easymock
testCompile libs.powermockJunit4
testCompile libs.powermockEasymock
Expand Down Expand Up @@ -1324,7 +1342,8 @@ project(':streams:streams-scala') {
testCompile project(':clients').sourceSets.test.output
testCompile project(':streams:test-utils')

testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.scalatest
testCompile libs.easymock
testCompile libs.hamcrest
Expand Down Expand Up @@ -1359,7 +1378,8 @@ project(':streams:test-utils') {
compile project(':clients')

testCompile project(':clients').sourceSets.test.output
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.easymock
testCompile libs.hamcrest

Expand Down Expand Up @@ -1395,7 +1415,8 @@ project(':streams:examples') {

testCompile project(':streams:test-utils')
testCompile project(':clients').sourceSets.test.output // for org.apache.kafka.test.IntegrationTest
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
}

javadoc {
Expand Down Expand Up @@ -1630,7 +1651,8 @@ project(':log4j-appender') {
compile libs.slf4jlog4j

testCompile project(':clients').sourceSets.test.output
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.easymock
}

Expand All @@ -1648,7 +1670,8 @@ project(':connect:api') {
compile libs.slf4jApi
compile libs.jaxrsApi

testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine

testRuntime libs.slf4jlog4j
testCompile project(':clients').sourceSets.test.output
Expand Down Expand Up @@ -1689,7 +1712,8 @@ project(':connect:transforms') {
compile libs.slf4jApi

testCompile libs.easymock
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.powermockJunit4
testCompile libs.powermockEasymock

Expand Down Expand Up @@ -1729,7 +1753,8 @@ project(':connect:json') {
compile libs.slf4jApi

testCompile libs.easymock
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.powermockJunit4
testCompile libs.powermockEasymock

Expand Down Expand Up @@ -1785,7 +1810,8 @@ project(':connect:runtime') {

testCompile project(':clients').sourceSets.test.output
testCompile libs.easymock
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.powermockJunit4
testCompile libs.powermockEasymock
testCompile libs.mockitoCore
Expand Down Expand Up @@ -1871,7 +1897,8 @@ project(':connect:file') {
compile libs.slf4jApi

testCompile libs.easymock
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.powermockJunit4
testCompile libs.powermockEasymock

Expand Down Expand Up @@ -1910,7 +1937,8 @@ project(':connect:basic-auth-extension') {

testCompile libs.bcpkix
testCompile libs.easymock
testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.powermockJunit4
testCompile libs.powermockEasymock
testCompile project(':clients').sourceSets.test.output
Expand Down Expand Up @@ -1952,7 +1980,8 @@ project(':connect:mirror') {
compile libs.argparse4j
compile libs.slf4jApi

testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile libs.mockitoCore
testCompile project(':clients').sourceSets.test.output
testCompile project(':connect:runtime').sourceSets.test.output
Expand Down Expand Up @@ -1992,7 +2021,8 @@ project(':connect:mirror-client') {
compile project(':clients')
compile libs.slf4jApi

testCompile libs.junit
testCompile libs.junitJupiterApi
testCompile libs.junitVintageEngine
testCompile project(':clients').sourceSets.test.output

testRuntime libs.slf4jlog4j
Expand Down
27 changes: 13 additions & 14 deletions clients/src/test/java/org/apache/kafka/test/TestUtils.java
Expand Up @@ -64,12 +64,11 @@
import java.util.regex.Pattern;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Helper functions for writing unit tests
Expand Down Expand Up @@ -98,7 +97,7 @@ public static Cluster singletonCluster(final String topic, final int partitions)
}

public static Cluster clusterWith(int nodes) {
return clusterWith(nodes, new HashMap<String, Integer>());
return clusterWith(nodes, new HashMap<>());
}

public static Cluster clusterWith(final int nodes, final Map<String, Integer> topicPartitionCounts) {
Expand Down Expand Up @@ -398,8 +397,8 @@ public static void waitForCondition(final TestCondition testCondition, final lon
retryOnExceptionWithTimeout(maxWaitMs, () -> {
String conditionDetailsSupplied = conditionDetailsSupplier != null ? conditionDetailsSupplier.get() : null;
String conditionDetails = conditionDetailsSupplied != null ? conditionDetailsSupplied : "";
assertThat("Condition not met within timeout " + maxWaitMs + ". " + conditionDetails,
testCondition.conditionMet());
assertTrue(testCondition.conditionMet(),
"Condition not met within timeout " + maxWaitMs + ". " + conditionDetails);
});
}

Expand Down Expand Up @@ -554,8 +553,8 @@ public static Set<TopicPartition> generateRandomTopicPartitions(int numTopic, in
*/
public static <T extends Throwable> T assertFutureThrows(Future<?> future, Class<T> exceptionCauseClass) {
ExecutionException exception = assertThrows(ExecutionException.class, future::get);
assertTrue("Unexpected exception cause " + exception.getCause(),
exceptionCauseClass.isInstance(exception.getCause()));
assertTrue(exceptionCauseClass.isInstance(exception.getCause()),
"Unexpected exception cause " + exception.getCause());
return exceptionCauseClass.cast(exception.getCause());
}

Expand All @@ -566,9 +565,9 @@ public static void assertFutureError(Future<?> future, Class<? extends Throwable
fail("Expected a " + exceptionClass.getSimpleName() + " exception, but got success.");
} catch (ExecutionException ee) {
Throwable cause = ee.getCause();
assertEquals("Expected a " + exceptionClass.getSimpleName() + " exception, but got " +
cause.getClass().getSimpleName(),
exceptionClass, cause.getClass());
assertEquals(exceptionClass, cause.getClass(),
"Expected a " + exceptionClass.getSimpleName() + " exception, but got " +
cause.getClass().getSimpleName());
}
}

Expand Down
Expand Up @@ -14,13 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package unit.kafka.cluster
package kafka.cluster

import java.io.File
import java.util.Properties

import kafka.api.ApiVersion
import kafka.cluster.{DelayedOperations, Partition, PartitionStateStore}
import kafka.log.{CleanerConfig, LogConfig, LogManager}
import kafka.server.{Defaults, MetadataCache}
import kafka.server.checkpoints.OffsetCheckpoints
Expand Down
Expand Up @@ -14,9 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package unit.kafka.cluster
package kafka.cluster

import kafka.cluster.SimpleAssignmentState
import org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState
import org.junit.Assert.{assertEquals, assertFalse, assertTrue}
import org.junit.Test
Expand Down
1 change: 0 additions & 1 deletion core/src/test/scala/unit/kafka/cluster/PartitionTest.scala
Expand Up @@ -42,7 +42,6 @@ import org.mockito.ArgumentMatchers
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
import org.scalatest.Assertions.assertThrows
import unit.kafka.cluster.AbstractPartitionTest

import scala.jdk.CollectionConverters._

Expand Down
3 changes: 1 addition & 2 deletions core/src/test/scala/unit/kafka/utils/ThrottlerTest.scala
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*/

package unit.kafka.utils
package kafka.utils

import kafka.utils.Throttler
import org.apache.kafka.common.utils.MockTime
import org.junit.Test
import org.junit.Assert.{assertTrue, assertEquals}
Expand Down
9 changes: 6 additions & 3 deletions gradle/dependencies.gradle
Expand Up @@ -78,7 +78,7 @@ versions += [
jaxrs: "2.1.1",
jfreechart: "1.0.0",
jopt: "5.0.4",
junit: "4.13",
junit: "5.7.0-RC1",
kafka_0100: "0.10.0.1",
kafka_0101: "0.10.1.1",
kafka_0102: "0.10.2.2",
Expand All @@ -95,7 +95,7 @@ versions += [
lz4: "1.7.1",
mavenArtifact: "3.6.3",
metrics: "2.2.0",
mockito: "3.4.4",
mockito: "3.5.7",
netty: "4.1.51.Final",
owaspDepCheckPlugin: "5.3.2.1",
powermock: "2.0.7",
Expand Down Expand Up @@ -151,7 +151,9 @@ libs += [
jmhCoreBenchmarks: "org.openjdk.jmh:jmh-core-benchmarks:$versions.jmh",
jmhGeneratorAnnProcess: "org.openjdk.jmh:jmh-generator-annprocess:$versions.jmh",
joptSimple: "net.sf.jopt-simple:jopt-simple:$versions.jopt",
junit: "junit:junit:$versions.junit",
junitJupiter: "org.junit.jupiter:junit-jupiter:$versions.junit",
junitJupiterApi: "org.junit.jupiter:junit-jupiter-api:$versions.junit",
junitVintageEngine: "org.junit.vintage:junit-vintage-engine:$versions.junit",
hamcrest: "org.hamcrest:hamcrest:$versions.hamcrest",
kafkaStreams_0100: "org.apache.kafka:kafka-streams:$versions.kafka_0100",
kafkaStreams_0101: "org.apache.kafka:kafka-streams:$versions.kafka_0101",
Expand All @@ -170,6 +172,7 @@ libs += [
lz4: "org.lz4:lz4-java:$versions.lz4",
metrics: "com.yammer.metrics:metrics-core:$versions.metrics",
mockitoCore: "org.mockito:mockito-core:$versions.mockito",
mockitoInline: "org.mockito:mockito-inline:$versions.mockito",
nettyHandler: "io.netty:netty-handler:$versions.netty",
nettyTransportNativeEpoll: "io.netty:netty-transport-native-epoll:$versions.netty",
powermockJunit4: "org.powermock:powermock-module-junit4:$versions.powermock",
Expand Down
Expand Up @@ -124,7 +124,7 @@ public static class KiboshControlFile {
private final List<KiboshFaultSpec> faults;

public final static KiboshControlFile EMPTY =
new KiboshControlFile(Collections.<KiboshFaultSpec>emptyList());
new KiboshControlFile(Collections.emptyList());

public static KiboshControlFile read(Path controlPath) throws IOException {
byte[] controlFileBytes = Files.readAllBytes(controlPath);
Expand All @@ -133,7 +133,7 @@ public static KiboshControlFile read(Path controlPath) throws IOException {

@JsonCreator
public KiboshControlFile(@JsonProperty("faults") List<KiboshFaultSpec> faults) {
this.faults = faults == null ? new ArrayList<KiboshFaultSpec>() : faults;
this.faults = faults == null ? new ArrayList<>() : faults;
}

@JsonProperty
Expand Down