Annotation Interface EmbeddedKafka
@ExtendWith(EmbeddedKafkaCondition.class)
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Inherited
@DisabledInAotMode
public @interface EmbeddedKafka
Annotation that can be specified on a test class that runs Spring for Apache Kafka
based tests.
Provides the following features over and above the regular Spring TestContext
Framework:
- Registers a
EmbeddedKafkaBroker
bean with theEmbeddedKafkaBroker.BEAN_NAME
bean name.
The typical usage of this annotation is like:
@RunWith(SpringRunner.class) @EmbeddedKafka public class MyKafkaTests { @Autowired private EmbeddedKafkaBroker kafkaEmbedded; @Value("${spring.embedded.kafka.brokers}") private String brokerAddresses; }When using EmbeddedKafka with
SpringJUnitConfig
,
it is recommended to use DirtiesContext
on the test class,
in order to prevent certain race conditions on JVM shutdown when running multiple tests.- Since:
- 1.3
- Author:
- Artem Bilan, Elliot Metsger, Zach Olauson, Gary Russell, Sergio Lourenco, Pawel Lozinski, Adrian Chlebosz, Soby Chacko
- See Also:
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionint
Timeout in seconds for admin operations (e.g.The property name to set with the bootstrap server addresses as well as the default "spring.embedded.kafka.brokers".String[]
Properties in form key=value that should be added to the broker config before runs.SpringResource
url specifying the location of properties that should be added to the broker config.boolean
int
boolean
Use KRaft instead of Zookeeper; default false.int
int[]
Set explicit ports on which the kafka brokers will listen.String[]
Topics that should be created Topics may contain property place holders, e.g.int
int
Timeout for internal ZK client connection.int
Timeout for internal ZK client session.int
Set the port on which the embedded Zookeeper should listen.
-
Element Details
-
value
- Returns:
- the number of brokers
- Default:
- 1
-
count
- Returns:
- the number of brokers
- Default:
- 1
-
controlledShutdown
boolean controlledShutdown- Returns:
- passed into
kafka.utils.TestUtils.createBrokerConfig()
.
- Default:
- false
-
ports
int[] portsSet explicit ports on which the kafka brokers will listen. Useful when running an embedded broker that you want to access from other processes. A port must be provided for each instance, which means the number of ports must match the value of the count attribute. This property is not valid when using KRaft mode.- Returns:
- ports for brokers.
- Since:
- 2.2.4
- Default:
- {0}
-
zookeeperPort
int zookeeperPortSet the port on which the embedded Zookeeper should listen. This property is not valid when using KRaft mode.- Returns:
- the port.
- Since:
- 2.3
- Default:
- 0
-
partitions
int partitions- Returns:
- partitions per topic
- Default:
- 2
-
topics
String[] topicsTopics that should be created Topics may contain property place holders, e.g.topics = "${kafka.topic.one:topicOne}"
The topics will be created withpartitions()
partitions; to provision other topics with other partition counts call theaddTopics(NewTopic... topics)
method on the autowired broker. Place holders will only be resolved when there is a Spring test application context present (such as when using@SpringJunitConfig or @SpringRunner
.- Returns:
- the topics to create
- Default:
- {}
-
brokerProperties
String[] brokerPropertiesProperties in form key=value that should be added to the broker config before runs. When used in a Spring test context, properties may contain property place holders, e.g.delete.topic.enable=${topic.delete:true}
. Place holders will only be resolved when there is a Spring test application context present (such as when using@SpringJunitConfig or @SpringRunner
.- Returns:
- the properties to add
- See Also:
- Default:
- {}
-
brokerPropertiesLocation
String brokerPropertiesLocationSpringResource
url specifying the location of properties that should be added to the broker config. When used in a Spring test context, thebrokerPropertiesLocation
url and the properties themselves may contain place holders that are resolved during initialization. Properties specified bybrokerProperties()
will override properties found inbrokerPropertiesLocation
. Place holders will only be resolved when there is a Spring test application context present (such as when using@SpringJunitConfig or @SpringRunner
.- Returns:
- a
Resource
url specifying the location of properties to add - See Also:
- Default:
- ""
-
bootstrapServersProperty
String bootstrapServersPropertyThe property name to set with the bootstrap server addresses as well as the default "spring.embedded.kafka.brokers". Defaults tospring.kafka.bootstrap-servers
since 3.0.10.- Returns:
- the property name.
- Since:
- 2.3
- See Also:
- Default:
- "spring.kafka.bootstrap-servers"
-
zkConnectionTimeout
int zkConnectionTimeoutTimeout for internal ZK client connection. This property is not valid when using KRaft mode.- Returns:
- default
EmbeddedKafkaZKBroker.DEFAULT_ZK_CONNECTION_TIMEOUT
. - Since:
- 2.4
- Default:
- 18000
-
zkSessionTimeout
int zkSessionTimeoutTimeout for internal ZK client session. This property is not valid when using KRaft mode.- Returns:
- default
EmbeddedKafkaZKBroker.DEFAULT_ZK_SESSION_TIMEOUT
. - Since:
- 2.4
- Default:
- 18000
-
adminTimeout
int adminTimeoutTimeout in seconds for admin operations (e.g. topic creation, close).- Returns:
- default
EmbeddedKafkaBroker.DEFAULT_ADMIN_TIMEOUT
- Since:
- 2.8.5
- Default:
- 10
-
kraft
boolean kraftUse KRaft instead of Zookeeper; default false.- Returns:
- whether to use KRaft.
- Since:
- 3.6
- Default:
- false
-