Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
add tests for blogpost initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Dec 20, 2023
1 parent e16d093 commit 172bd56
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -27,6 +30,8 @@ void shouldCreateBlogPostSuccessfully() {
assertThat(result.getBody().getTitle())
.isEqualTo(blogPost.getTitle());

// TODO verify eventPublisher.publishEvent?

}

}
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();
}
}

0 comments on commit 172bd56

Please sign in to comment.