Skip to content

Commit 9655a97

Browse files
NO-ISSUE: Refactoring test codes to use @embeddedkafka
Signed-off-by: chickenchickenlove <[email protected]>
1 parent a52facc commit 9655a97

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

spring-kafka/src/test/java/org/springframework/kafka/annotation/AliasPropertiesTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import org.springframework.kafka.core.KafkaTemplate;
4747
import org.springframework.kafka.core.ProducerFactory;
4848
import org.springframework.kafka.test.EmbeddedKafkaBroker;
49-
import org.springframework.kafka.test.EmbeddedKafkaKraftBroker;
49+
import org.springframework.kafka.test.context.EmbeddedKafka;
5050
import org.springframework.kafka.test.utils.KafkaTestUtils;
5151
import org.springframework.stereotype.Component;
5252
import org.springframework.test.annotation.DirtiesContext;
@@ -58,12 +58,14 @@
5858
* @author Gary Russell
5959
* @author Artem Bilan
6060
* @author Soby Chacko
61+
* @author Sanghyeok An
6162
*
6263
* @since 2.2
6364
*
6465
*/
6566
@SpringJUnitConfig
6667
@DirtiesContext
68+
@EmbeddedKafka(partitions = 1, topics = "alias.tests")
6769
public class AliasPropertiesTests {
6870

6971
@Autowired
@@ -109,6 +111,9 @@ public void testAliasFor() throws Exception {
109111
@EnableKafka
110112
public static class Config {
111113

114+
@Autowired
115+
EmbeddedKafkaBroker broker;
116+
112117
final CountDownLatch latch = new CountDownLatch(1);
113118

114119
static AtomicBoolean orderedCalledFirst = new AtomicBoolean(true);
@@ -137,11 +142,6 @@ public KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry() {
137142
return new KafkaListenerEndpointRegistry();
138143
}
139144

140-
@Bean
141-
public EmbeddedKafkaBroker embeddedKafka() {
142-
return new EmbeddedKafkaKraftBroker(1, 1, "alias.tests");
143-
}
144-
145145
@Bean
146146
public KafkaListenerContainerFactory<?> kafkaListenerContainerFactory() {
147147
ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
@@ -158,7 +158,7 @@ public DefaultKafkaConsumerFactory<Integer, String> consumerFactory() {
158158
@Bean
159159
public Map<String, Object> consumerConfigs() {
160160
Map<String, Object> consumerProps =
161-
KafkaTestUtils.consumerProps("myAliasGroup", "false", embeddedKafka());
161+
KafkaTestUtils.consumerProps(this.broker.getBrokersAsString(), "myAliasGroup", "false");
162162
return consumerProps;
163163
}
164164

@@ -174,7 +174,7 @@ public ProducerFactory<Integer, String> producerFactory() {
174174

175175
@Bean
176176
public Map<String, Object> producerConfigs() {
177-
return KafkaTestUtils.producerProps(embeddedKafka());
177+
return KafkaTestUtils.producerProps(this.broker.getBrokersAsString());
178178
}
179179

180180
@MyListener(id = "onMethodInConfigClass", value = "alias.tests")

spring-kafka/src/test/java/org/springframework/kafka/listener/ErrorHandlingDeserializerTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import org.springframework.kafka.support.serializer.ErrorHandlingDeserializer;
5252
import org.springframework.kafka.support.serializer.SerializationUtils;
5353
import org.springframework.kafka.test.EmbeddedKafkaBroker;
54-
import org.springframework.kafka.test.EmbeddedKafkaKraftBroker;
54+
import org.springframework.kafka.test.context.EmbeddedKafka;
5555
import org.springframework.kafka.test.utils.KafkaTestUtils;
5656
import org.springframework.test.annotation.DirtiesContext;
5757
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -62,14 +62,16 @@
6262

6363
/**
6464
* @author Gary Russell
65+
* @author Sanghyeok An
6566
* @since 2.2
6667
*
6768
*/
6869
@SpringJUnitConfig
6970
@DirtiesContext
71+
@EmbeddedKafka(partitions = 1, topics = ErrorHandlingDeserializerTests.TOPIC)
7072
public class ErrorHandlingDeserializerTests {
7173

72-
private static final String TOPIC = "ehdt";
74+
static final String TOPIC = "ehdt";
7375

7476
@Autowired
7577
public Config config;
@@ -180,6 +182,9 @@ public boolean supports(Class<?> clazz) {
180182
@EnableKafka
181183
public static class Config {
182184

185+
@Autowired
186+
EmbeddedKafkaBroker broker;
187+
183188
private final CountDownLatch latch = new CountDownLatch(6);
184189

185190
private final AtomicInteger goodCount = new AtomicInteger();
@@ -202,11 +207,6 @@ public void listen2(ConsumerRecord<String, String> record) {
202207
this.latch.countDown();
203208
}
204209

205-
@Bean
206-
public EmbeddedKafkaBroker embeddedKafka() {
207-
return new EmbeddedKafkaKraftBroker(1, 1, TOPIC);
208-
}
209-
210210
@Bean
211211
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
212212
return factory(cf());
@@ -245,7 +245,7 @@ else if (r.key() == null && t.getCause() instanceof DeserializationException) {
245245

246246
@Bean
247247
public ConsumerFactory<String, String> cf() {
248-
Map<String, Object> props = KafkaTestUtils.consumerProps(TOPIC + ".g1", "false", embeddedKafka());
248+
Map<String, Object> props = KafkaTestUtils.consumerProps(this.broker.getBrokersAsString(), TOPIC + ".g1", "false");
249249
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ExtendedEHD.class.getName());
250250
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class);
251251
props.put(ErrorHandlingDeserializer.KEY_DESERIALIZER_CLASS, FailSometimesDeserializer.class);
@@ -255,15 +255,15 @@ public ConsumerFactory<String, String> cf() {
255255

256256
@Bean
257257
public ConsumerFactory<String, String> cfWithExplicitDeserializers() {
258-
Map<String, Object> props = KafkaTestUtils.consumerProps(TOPIC + ".g2", "false", embeddedKafka());
258+
Map<String, Object> props = KafkaTestUtils.consumerProps(this.broker.getBrokersAsString(), TOPIC + ".g2", "false");
259259
return new DefaultKafkaConsumerFactory<>(props,
260260
new ErrorHandlingDeserializer<String>(new FailSometimesDeserializer()).keyDeserializer(true),
261261
new ErrorHandlingDeserializer<String>(new FailSometimesDeserializer()));
262262
}
263263

264264
@Bean
265265
public ProducerFactory<String, String> pf() {
266-
Map<String, Object> props = KafkaTestUtils.producerProps(embeddedKafka());
266+
Map<String, Object> props = KafkaTestUtils.producerProps(broker.getBrokersAsString());
267267
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
268268
return new DefaultKafkaProducerFactory<>(props);
269269
}

0 commit comments

Comments
 (0)