Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@EmbeddedKafkaRule to @EmbeddedKafka test conversion #75

Open
pway99 opened this issue Jun 2, 2021 · 1 comment
Open

@EmbeddedKafkaRule to @EmbeddedKafka test conversion #75

pway99 opened this issue Jun 2, 2021 · 1 comment
Labels
recipe Recipe requested

Comments

@pway99
Copy link
Contributor

pway99 commented Jun 2, 2021

@EmbeddedKafkaRule to @EmbeddedKafka for JUnit 5 tests migration recipe.

https://docs.spring.io/spring-kafka/docs/current/api/org/springframework/kafka/test/context/EmbeddedKafka.html
https://blog.mimacom.com/testing-apache-kafka-with-spring-boot-junit5/

@pway99 pway99 added the enhancement New feature or request label Jun 2, 2021
@pway99
Copy link
Contributor Author

pway99 commented Jun 2, 2021

example from spring-batch KafkaItemReaderTests.

  1. remove EmbeddedKafkaRule
  2. add class annotations
    @EmbeddedKafka
    @ExtendWith(SpringExtension.class)
    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
  3. add
    @Autowired
    private EmbeddedKafkaBroker embeddedKafkaBroker;
  4. remove static modifier from setup() method (note. @TestInstance annotation).
  5. replace embeddedKafka.getEmbeddedKafka() with embeddedKafkaBroker

before:

public class KafkaItemReaderTests {

	@ClassRule
	public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1);

	private KafkaItemReader<String, String> reader;
	private KafkaTemplate<String, String> template;
	private Properties consumerProperties;

	@BeforeClass
	public static void setUpTopics() {
		embeddedKafka.getEmbeddedKafka().addTopics(
				new NewTopic("topic1", 1, (short) 1),
				new NewTopic("topic2", 2, (short) 1),
				new NewTopic("topic3", 1, (short) 1),
				new NewTopic("topic4", 2, (short) 1),
				new NewTopic("topic5", 1, (short) 1),
				new NewTopic("topic6", 1, (short) 1)
		);
	}

	@Before
	public void setUp() {
		Map<String, Object> producerProperties = KafkaTestUtils.producerProps(embeddedKafka.getEmbeddedKafka());
		ProducerFactory<String, String> producerFactory = new DefaultKafkaProducerFactory<>(producerProperties);
		this.template = new KafkaTemplate<>(producerFactory);

		this.consumerProperties = new Properties();
		this.consumerProperties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
				embeddedKafka.getEmbeddedKafka().getBrokersAsString());
		this.consumerProperties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "1");
		this.consumerProperties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
				StringDeserializer.class.getName());
		this.consumerProperties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
				StringDeserializer.class.getName());
	}
}

after:

@EmbeddedKafka
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class KafkaItemReaderTests {

	@Autowired
	private EmbeddedKafkaBroker embeddedKafkaBroker;

	private KafkaItemReader<String, String> reader;
	private KafkaTemplate<String, String> template;
	private Properties consumerProperties;

	@BeforeAll
	public void setUpTopics() {
		embeddedKafkaBroker.addTopics(
				new NewTopic("topic1", 1, (short) 1),
				new NewTopic("topic2", 2, (short) 1),
				new NewTopic("topic3", 1, (short) 1),
				new NewTopic("topic4", 2, (short) 1),
				new NewTopic("topic5", 1, (short) 1),
				new NewTopic("topic6", 1, (short) 1)
		);
	}

	@BeforeEach
	public void setUp() {
		Map<String, Object> producerProperties = KafkaTestUtils.producerProps(embeddedKafkaBroker);
		ProducerFactory<String, String> producerFactory = new DefaultKafkaProducerFactory<>(producerProperties);
		this.template = new KafkaTemplate<>(producerFactory);

		this.consumerProperties = new Properties();
		this.consumerProperties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
				embeddedKafkaBroker.getBrokersAsString());
		this.consumerProperties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "1");
		this.consumerProperties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
				StringDeserializer.class.getName());
		this.consumerProperties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
				StringDeserializer.class.getName());
	}
}

@timtebeek timtebeek moved this to Recipes Wanted in OpenRewrite Nov 29, 2023
@timtebeek timtebeek added recipe Recipe requested and removed enhancement New feature or request labels Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants