BAEL-7574: added tests

This commit is contained in:
emanueltrandafir1993 2024-03-21 23:26:12 +01:00 committed by emanuel.trandafir
parent 4a95314449
commit 5e9a5b9b3f
5 changed files with 109 additions and 3 deletions

View File

@ -2,5 +2,5 @@ package com.baeldung.springmodulith.application.events.orders;
import java.time.Instant;
public record OrderCompletedEvent(String orderId, String customerId, Instant time) {
public record OrderCompletedEvent(String orderId, String customerId, Instant timestamp) {
}

View File

@ -14,8 +14,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events")
public class ApplicationEventsTest {
public class ApplicationEventsUnitTest {
@Autowired
OrderService orderService;

View File

@ -0,0 +1,39 @@
package com.baeldung.springmodulith.application.events;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Instant;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.ComponentScan;
import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent;
import com.baeldung.springmodulith.application.events.rewards.LoyalCustomersRepository;
@SpringBootTest
@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events")
public class EventListenerUnitTest {
@Autowired
private LoyalCustomersRepository customers;
@Autowired
private ApplicationEventPublisher testEventPublisher;
@Test
void whenPublishingOrderCompletedEvent_thenRewardCustomerWithLoyaltyPoints() {
OrderCompletedEvent event = new OrderCompletedEvent("order-1", "customer-1", Instant.now());
testEventPublisher.publishEvent(event);
assertThat(customers.find("customer-1"))
.isPresent().get()
.hasFieldOrPropertyWithValue("customerId", "customer-1")
.hasFieldOrPropertyWithValue("points", 60);
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.springmodulith.application.events;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import com.baeldung.springmodulith.application.events.orders.OrderService;
@SpringBootTest
@ComponentScan(basePackages = "com.baeldung.springmodulith.application.events")
public class EventPublisherUnitTest {
@Autowired
OrderService orderService;
@Autowired
TestEventListener testEventListener;
@BeforeEach
void beforeEach() {
testEventListener.reset();
}
@Test
void whenPlacingOrder_thenPublishApplicationEvent() {
orderService.placeOrder("customer1", "product1", "product2");
assertThat(testEventListener.getEvents())
.hasSize(1).first()
.hasFieldOrPropertyWithValue("customerId", "customer1")
.hasFieldOrProperty("orderId")
.hasFieldOrProperty("timestamp");
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.springmodulith.application.events;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent;
@Component
class TestEventListener {
private final List<OrderCompletedEvent> events = new ArrayList<>();
@EventListener
void onEvent(OrderCompletedEvent event) {
events.add(event);
}
public List<OrderCompletedEvent> getEvents() {
return events;
}
public void reset() {
events.clear();
}
}