This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for blogpost initialization
- Loading branch information
Ralf Ueberfuhr
committed
Dec 20, 2023
1 parent
e16d093
commit 172bd56
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/de/sample/schulung/spring/blog/domain/BlogPostEventsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |