Class KafkaClusterExtension
java.lang.Object
io.kroxylicious.testing.kafka.junit5ext.KafkaClusterExtension
- All Implemented Interfaces:
BeforeAllCallback,BeforeEachCallback,Extension,ParameterResolver,TestTemplateInvocationContextProvider
public class KafkaClusterExtension
extends Object
implements ParameterResolver, BeforeEachCallback, BeforeAllCallback, TestTemplateInvocationContextProvider
A JUnit 5 extension that allows declarative injection of a
KafkaCluster into a test
via static or instance field(s) and/or parameters.
A simple example looks like:
import io.kroxylicious.junit5.KafkaClusterExtension;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith(KafkaClusterExtension.class) // <1>
class MyTest {
KafkaCluster cluster; // <2>
@Test
public void testProducer(
Producer<String, String> producer // <3>
) throws Exception {
producer.send(new ProducerRecord<>("hello", "world")).get();
}
}
Notes:
- You have to tell Junit that you're using the extension using
@ExtendWith. - An instance field of type
KafkaClusterwill cause a new cluster to be provisioned for each test in the class. Alternatively you can use a parameter on a@Test-annotated method. If you use astaticfield then a single cluster will be provisioned for all the tests in the class. - Your test methods can declare
Producer,ConsumerandAdmin-typed parameters. They will be configured to bootstrap against thecluster.
Injection rules
The extension supports injecting clusters and clients:
- into fields of the test class
- as parameters to
@BeforeAll - as parameters to
@BeforeEach - as parameters to test methods
- have no annotations
- OR are annotated with annotations from the following packages
java.langorg.junitio.kroxylicious
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbeforeAll(ExtensionContext context) Perform field injection for non-private, static fields of typeKafkaClusterorKafkaCluster.voidbeforeEach(ExtensionContext context) Perform field injection for non-private, instance fields of typeKafkaClusterorKafkaCluster.resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) static ObjectresolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext, List<Annotation> extraConstraints) Resolve parameter object.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) booleansupportsTestTemplate(ExtensionContext context)
-
Field Details
-
STARTING_PREFIX
The constant STARTING_PREFIX.- See Also:
-
-
Constructor Details
-
KafkaClusterExtension
public KafkaClusterExtension()Instantiates a new Kafka cluster extension.
-
-
Method Details
-
supportsTestTemplate
- Specified by:
supportsTestTemplatein interfaceTestTemplateInvocationContextProvider
-
provideTestTemplateInvocationContexts
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) - Specified by:
provideTestTemplateInvocationContextsin interfaceTestTemplateInvocationContextProvider
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException - Specified by:
supportsParameterin interfaceParameterResolver- Throws:
ParameterResolutionException
-
resolveParameter
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException - Specified by:
resolveParameterin interfaceParameterResolver- Throws:
ParameterResolutionException
-
resolveParameter
public static Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext, List<Annotation> extraConstraints) throws ParameterResolutionException Resolve parameter object.- Parameters:
parameterContext- the parameter contextextensionContext- the extension contextextraConstraints- the extra constraints- Returns:
- the object
- Throws:
ParameterResolutionException- the parameter resolution exception
-
beforeAll
Perform field injection for non-private, static fields of typeKafkaClusterorKafkaCluster.- Specified by:
beforeAllin interfaceBeforeAllCallback- Throws:
Exception
-
beforeEach
Perform field injection for non-private, instance fields of typeKafkaClusterorKafkaCluster.- Specified by:
beforeEachin interfaceBeforeEachCallback- Throws:
Exception
-