Class MiniClusterExtension

java.lang.Object
org.apache.flink.test.junit5.MiniClusterExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver

@Experimental public final class MiniClusterExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.ParameterResolver
Starts a Flink MiniCluster and registers the StreamExecutionEnvironment in the correct thread local environment.

Example usage:


 @ExtendWith(MiniClusterExtension.class)
 class MyTest {
      @Test
      public void myTest() {
          ExecutionEnvironment execEnv = ExecutionEnvironment.getExecutionEnvironment();
      }
 }
 

Or to tune the MiniCluster parameters:


 class MyTest {

     @RegisterExtension
     public static final MiniClusterExtension MINI_CLUSTER_RESOURCE =
             new MiniClusterExtension(
                     new MiniClusterResourceConfiguration.Builder()
                             .setNumberTaskManagers(1)
                             .setConfiguration(new Configuration())
                             .build());

     @Test
     public void myTest() {
          ExecutionEnvironment execEnv = ExecutionEnvironment.getExecutionEnvironment();
     }
 }
 

You can use parameter injection with the annotations InjectMiniCluster, InjectClusterClient, InjectClusterRESTAddress, InjectClusterClientConfiguration:


 class MyTest {

     @RegisterExtension
     public static final MiniClusterExtension MINI_CLUSTER_RESOURCE =
             new MiniClusterExtension(
                     new MiniClusterResourceConfiguration.Builder()
                             .setNumberTaskManagers(1)
                             .setConfiguration(new Configuration())
                             .build());

     @Test
     public void myTest(@InjectMiniCluster MiniCluster miniCluster) {
          // Use miniCluster
     }

     @Test
     public void myTest(@InjectClusterClient ClusterClient<?> clusterClient) {
          // clusterClient here is an instance of MiniClusterClient
     }

     @Test
     public void myTest(@InjectClusterClient RestClusterClient<?> restClusterClient) {
          // Using RestClusterClient as parameter type will force the creation of a RestClusterClient,
          //  rather than MiniClusterClient
     }
 }
 

You can use it both with programmatic and declarative extension annotations. Check <a href="https://junit.org/junit5/docs/snapshot/user-guide/#extensions-registration>JUnit 5 docs for more details.

This extension by default creates one MiniCluster per test class. If you need one instance of MiniCluster per test, we suggest you to instantiate it manually in your test code.

This extension works correctly with parallel tests and with parametrized tests, but it doesn't work when combining TestFactory and parallel tests, because of some limitations. Use ParameterizedTest instead.

  • Constructor Details

    • MiniClusterExtension

      public MiniClusterExtension()
    • MiniClusterExtension

      public MiniClusterExtension(org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration miniClusterResourceConfiguration)
    • MiniClusterExtension

      @Experimental public MiniClusterExtension(Supplier<org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration> miniClusterResourceConfigurationSupplier)
  • Method Details

    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
      Throws:
      Exception
    • beforeEach

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

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
      Throws:
      Exception
    • getTestStreamEnvironment

      public TestStreamEnvironment getTestStreamEnvironment()
    • getClientConfiguration

      public org.apache.flink.configuration.Configuration getClientConfiguration()
    • getNumberSlots

      public Integer getNumberSlots()
    • isRunning

      public boolean isRunning()