Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Service Test (event-driven)
@SpringBootTest
@EmbeddedKafka(topics = {"orders", "payments"})
public class PaymentServiceTest {
@Autowired
private Producer<Order> orderProducer;
@Autowired
private Consumer<Payment> paymentConsumer;
@Test
public void shouldProcessPaymentForValidOrder() {
var validOrder = Order
.withCustomerId(123)
.withItems(Item.withId("af12da"));
orderProducer.send(validOrder);
var payment = paymentConsumer.poll();
assertThat(payment.getPaymentStatus()).isEqualTo("OK");
assertThat(payment.getPaymentTransactionId()).isNotNull();
}
@Test
public void shouldRejectPaymentForOrderWithMissingCustomer() {
var validOrder = Order
.withItems(Item.withId("af12da"));
orderProducer.send(validOrder);
var payment = paymentConsumer.poll();
assertThat(payment.getPaymentStatus()).isEqualTo("ORDER_INVALID");
assertThat(payment.getPaymentTransactionId()).isNull();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment