Class ClusterTestExtensions

java.lang.Object
org.apache.kafka.common.test.junit.ClusterTestExtensions
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider

public class ClusterTestExtensions extends Object implements org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
This class is a custom JUnit extension that will generate some number of test invocations depending on the processing of a few custom annotations. These annotations are placed on so-called test template methods. Template methods look like normal JUnit test methods, but instead of being invoked directly, they are used as templates for generating multiple test invocations. Test class that use this extension should use one of the following annotations on each template method:
  • ClusterTest, define a single cluster configuration
  • ClusterTests, provide multiple instances of @ClusterTest
  • ClusterTemplate, define a static method that generates cluster configurations
Any combination of these annotations may be used on a given test template method. If no test invocations are generated after processing the annotations, an error is thrown. Depending on which annotations are used, and what values are given, different ClusterConfig will be generated. Each ClusterConfig is used to create an underlying Kafka cluster that is used for the actual test invocation. For example:
 class SomeIntegrationTest {
   @ClusterTest(brokers = 1, controllers = 1, clusterType = ClusterType.Both)
   def someTest(): Unit = {
     assertTrue(condition)
   }
 }
 
will generate two invocations of "someTest" (since ClusterType.Both was given). For each invocation, the test class SomeIntegrationTest will be instantiated, lifecycle methods (before/after) will be run, and "someTest" will be invoked.
  • Constructor Details

    • ClusterTestExtensions

      public ClusterTestExtensions()
  • Method Details

    • supportsTestTemplate

      public boolean supportsTestTemplate(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      supportsTestTemplate in interface org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider
    • mayReturnZeroTestTemplateInvocationContexts

      public boolean mayReturnZeroTestTemplateInvocationContexts(org.junit.jupiter.api.extension.ExtensionContext context)
      Indicates whether this provider may return zero test template invocation contexts. Prior to JUnit 5.12, returning zero contexts was silently allowed. Starting from JUnit 5.12, a PreconditionViolationException is thrown unless this method explicitly returns true.
      Specified by:
      mayReturnZeroTestTemplateInvocationContexts in interface org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider
      Parameters:
      context - the extension context for the test template method
      Returns:
      true to allow this provider to return zero invocation contexts
      See Also:
    • provideTestTemplateInvocationContexts

      public Stream<org.junit.jupiter.api.extension.TestTemplateInvocationContext> provideTestTemplateInvocationContexts(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      provideTestTemplateInvocationContexts in interface org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback