mumrah commented on code in PR #18662:
URL: https://github.com/apache/kafka/pull/18662#discussion_r1975512220


##########
test-common/test-common-runtime/src/test/java/org/apache/kafka/common/test/junit/ClusterTestBeforeEachTest.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.common.test.junit;
+
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.AutoStart;
+import org.apache.kafka.common.test.api.ClusterTest;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@ExtendWith(ClusterTestExtensions.class)
+public class ClusterTestBeforeEachTest {
+    private final ClusterInstance clusterInstance;
+
+    ClusterTestBeforeEachTest(ClusterInstance clusterInstance) {     // 
Constructor injections
+        this.clusterInstance = clusterInstance;
+    }
+
+    @BeforeEach
+    void before() {
+        assertNotNull(clusterInstance);
+        if (!clusterInstance.started()) {

Review Comment:
   The use case that inspired this was from @smjn's PR. He had some common 
setup code that wanted to create topics and such in a `@BeforeEach`. However, 
we were using `BeforeTestExecutionCallback` as the point where we start the 
cluster. This extension point is too late in the test lifecycle to actually use 
ClusterInstace in `@BeforeEach`
   
   Paraphrasing the JUnit docs:
   
   * BeforeEachCallback: Callback that is invoked before each actual test 
method plus any user defined setup methods (e.g., `@BeforeEach` methods).
   * BeforeTestExecutionCallback: Callback that is invoked immediately before 
each test is executed.
   
   For his PR, he ended up doing what you suggest. Something like:
   
   ```
   @ClusterTest
   public void testOne(ClusterInstance cluster) {
       doCommonSetup(cluster);
       // Test code
   }
   
   @ClusterTest
   public void testTwo(ClusterInstance cluster) {
       doCommonSetup(cluster);
       // Test code
   }
   ```
   
   Where something like this was desired:
   
   ```
   @BeforeEach
   public void before() {
     // Common setup code with started ClusterInstance
   }
   
   @ClusterTest
   public void testOne(ClusterInstance cluster) {
       // Test code
   }
   
   @ClusterTest
   public void testTwo(ClusterInstance cluster) {
       // Test code
   }
   ```



-- 
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

Reply via email to