chia7712 commented on code in PR #15862: URL: https://github.com/apache/kafka/pull/15862#discussion_r1596214008
########## core/src/test/java/kafka/test/junit/ClusterTestExtensionsUnitTest.java: ########## @@ -18,31 +18,53 @@ package kafka.test.junit; import kafka.test.annotation.ClusterTemplate; +import kafka.test.ClusterGenerator; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.TestTemplateInvocationContext; -import java.util.function.Consumer; +import java.lang.reflect.Method; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class ClusterTestExtensionsUnitTest { + + static class StubTest { + @ClusterTemplate("cfgFoo") + void testFoo() {} + static void cfgFoo(ClusterGenerator gen) { /* ... */ } + + @ClusterTemplate("") + void testBar() {} + + }; + + private ExtensionContext buildExtensionContext(String methodName) throws Exception { Review Comment: please add `@SuppressWarnings({"unchecked", "rawtypes"})` ########## core/src/test/java/kafka/test/junit/ClusterTestExtensionsUnitTest.java: ########## @@ -18,31 +18,53 @@ package kafka.test.junit; import kafka.test.annotation.ClusterTemplate; +import kafka.test.ClusterGenerator; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.TestTemplateInvocationContext; -import java.util.function.Consumer; +import java.lang.reflect.Method; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class ClusterTestExtensionsUnitTest { + + static class StubTest { + @ClusterTemplate("cfgFoo") + void testFoo() {} + static void cfgFoo(ClusterGenerator gen) { /* ... */ } + + @ClusterTemplate("") + void testBar() {} + + }; + + private ExtensionContext buildExtensionContext(String methodName) throws Exception { + ExtensionContext extensionContext = mock(ExtensionContext.class); + Class clazz = StubTest.class; + Method method = clazz.getDeclaredMethod(methodName); + when(extensionContext.getRequiredTestClass()).thenReturn(clazz); + when(extensionContext.getRequiredTestMethod()).thenReturn(method); + return extensionContext; + } @Test @SuppressWarnings("unchecked") Review Comment: this is unused now. ########## core/src/test/java/kafka/test/junit/ClusterTestExtensionsUnitTest.java: ########## @@ -18,31 +18,53 @@ package kafka.test.junit; import kafka.test.annotation.ClusterTemplate; +import kafka.test.ClusterGenerator; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.TestTemplateInvocationContext; -import java.util.function.Consumer; +import java.lang.reflect.Method; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class ClusterTestExtensionsUnitTest { + + static class StubTest { + @ClusterTemplate("cfgFoo") + void testFoo() {} + static void cfgFoo(ClusterGenerator gen) { /* ... */ } + + @ClusterTemplate("") + void testBar() {} + + }; + + private ExtensionContext buildExtensionContext(String methodName) throws Exception { + ExtensionContext extensionContext = mock(ExtensionContext.class); + Class clazz = StubTest.class; + Method method = clazz.getDeclaredMethod(methodName); + when(extensionContext.getRequiredTestClass()).thenReturn(clazz); + when(extensionContext.getRequiredTestMethod()).thenReturn(method); + return extensionContext; + } @Test @SuppressWarnings("unchecked") void testProcessClusterTemplate() { - ClusterTestExtensions ext = new ClusterTestExtensions(); - ExtensionContext context = mock(ExtensionContext.class); - Consumer<TestTemplateInvocationContext> testInvocations = mock(Consumer.class); - ClusterTemplate annot = mock(ClusterTemplate.class); - when(annot.value()).thenReturn("").thenReturn(" "); - - Assertions.assertThrows(IllegalStateException.class, () -> - ext.processClusterTemplate(context, annot, testInvocations) + ClusterTestExtensions clusterTestExtensions = new ClusterTestExtensions(); + + assertEquals("ClusterConfig generator method should provide at least one config.", + assertThrows(IllegalStateException.class, () -> + clusterTestExtensions.provideTestTemplateInvocationContexts(buildExtensionContext("testFoo")) + ).getMessage() ); - Assertions.assertThrows(IllegalStateException.class, () -> - ext.processClusterTemplate(context, annot, testInvocations) + assertEquals("ClusterTemplate value can't be empty string.", Review Comment: Could we move this test case to new method `testEmptyClusterTemplate`? ########## core/src/test/java/kafka/test/junit/ClusterTestExtensions.java: ########## @@ -91,10 +91,7 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex // Process the @ClusterTemplate annotation ClusterTemplate clusterTemplateAnnot = context.getRequiredTestMethod().getDeclaredAnnotation(ClusterTemplate.class); if (clusterTemplateAnnot != null) { - processClusterTemplate(context, clusterTemplateAnnot, generatedContexts::add); Review Comment: We don't need those changes in this PR, right? That can be addressed in https://issues.apache.org/jira/browse/KAFKA-16654 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org