diff --git a/src/test/java/de/sample/schulung/spring/blog/boundary/BlogPostControllerTests.java b/src/test/java/de/sample/schulung/spring/blog/boundary/BlogPostControllerTests.java index 611e0c0..1100257 100644 --- a/src/test/java/de/sample/schulung/spring/blog/boundary/BlogPostControllerTests.java +++ b/src/test/java/de/sample/schulung/spring/blog/boundary/BlogPostControllerTests.java @@ -3,6 +3,8 @@ import de.sample.schulung.spring.blog.domain.BlogPostService; import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; +import org.mockito.Mockito; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.HttpStatus; import static org.assertj.core.api.Assertions.assertThat; @@ -11,7 +13,8 @@ public class BlogPostControllerTests { @Test void shouldCreateBlogPostSuccessfully() { - final var service = new BlogPostService(); + final var eventPublisher = Mockito.mock(ApplicationEventPublisher.class); + final var service = new BlogPostService(eventPublisher); final var mapper = Mappers.getMapper(BlogPostDtoMapper.class); final var controller = new BlogPostController(service, mapper); final var blogPost = new BlogPostDto(); @@ -27,6 +30,8 @@ void shouldCreateBlogPostSuccessfully() { assertThat(result.getBody().getTitle()) .isEqualTo(blogPost.getTitle()); + // TODO verify eventPublisher.publishEvent? + } } diff --git a/src/test/java/de/sample/schulung/spring/blog/domain/BlogPostEventsTests.java b/src/test/java/de/sample/schulung/spring/blog/domain/BlogPostEventsTests.java new file mode 100644 index 0000000..90ffd61 --- /dev/null +++ b/src/test/java/de/sample/schulung/spring/blog/domain/BlogPostEventsTests.java @@ -0,0 +1,50 @@ +package de.sample.schulung.spring.blog.domain; + +import jakarta.validation.ConstraintViolationException; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.event.ApplicationEvents; +import org.springframework.test.context.event.RecordApplicationEvents; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@SpringBootTest +@RecordApplicationEvents +public class BlogPostEventsTests { + + @Autowired + BlogPostService service; + @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") + @Autowired + ApplicationEvents events; + + @Test + void shouldPublishEventOnCreate() { + final var newBlogPost = BlogPost + .builder() + .title("test") + .content("test content with 10 chars") + .build(); + service.create(newBlogPost); + + assertThat(events.stream(BlogPostCreatedEvent.class)) + .hasSize(1) + .first() + .extracting(BlogPostCreatedEvent::blogPost) + .isSameAs(newBlogPost); + } + + @Test + void shouldNotPublishEventOnInvalidCreate() { + final var newBlogPost = BlogPost + .builder() + .content("test content with 10 chars") + .build(); + assertThatThrownBy(() -> service.create(newBlogPost)) + .isInstanceOf(ConstraintViolationException.class); + assertThat(events.stream(BlogPostCreatedEvent.class)) + .isEmpty(); + } +}