|
| 1 | +package dev.nano.springboottesting.service; |
| 2 | + |
| 3 | +import dev.nano.springboottesting.entity.Post; |
| 4 | +import dev.nano.springboottesting.repository.PostRepository; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 7 | +import org.mockito.ArgumentCaptor; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.boot.test.context.SpringBootTest; |
| 10 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 11 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 12 | + |
| 13 | +import java.util.Date; |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 18 | +import static org.mockito.ArgumentMatchers.any; |
| 19 | +import static org.mockito.BDDMockito.given; |
| 20 | +import static org.mockito.Mockito.never; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | + |
| 23 | +@ExtendWith(SpringExtension.class) |
| 24 | +@SpringBootTest |
| 25 | +class PostServiceTest { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private PostService underTest; |
| 29 | + |
| 30 | + @MockBean |
| 31 | + private PostRepository postRepository; |
| 32 | + |
| 33 | + @Test |
| 34 | + void canGetAllPostsSuccessfully() { |
| 35 | + // When |
| 36 | + underTest.findAllPosts(); |
| 37 | + // Then |
| 38 | + verify(postRepository).findAll(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void canCreateNewPostSuccessfully() { |
| 43 | + // Given |
| 44 | + Post post = new Post( |
| 45 | + 1L, |
| 46 | + "Post 1", |
| 47 | + "This is the description of post 1", |
| 48 | + new Date(), |
| 49 | + 1 |
| 50 | + ); |
| 51 | + // When |
| 52 | + underTest.savePost(post); |
| 53 | + // Then |
| 54 | + ArgumentCaptor<Post> postArgumentCaptor = ArgumentCaptor.forClass(Post.class); |
| 55 | + verify(postRepository).save(postArgumentCaptor.capture()); |
| 56 | + Post postReturned = postArgumentCaptor.getValue(); |
| 57 | + assertThat(postReturned).isEqualTo(post); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void canDeletePost() { |
| 62 | + // Given |
| 63 | + long postId = 1L; |
| 64 | + Post postMock = new Post( |
| 65 | + postId, |
| 66 | + "Post 1", |
| 67 | + "This is the description of post 1", |
| 68 | + new Date(), |
| 69 | + 1 |
| 70 | + ); |
| 71 | + given(postRepository.findById(postId)).willReturn(Optional.of(postMock)); |
| 72 | + // When |
| 73 | + underTest.deletePost(postId); |
| 74 | + // Then |
| 75 | + verify(postRepository).deleteById(postId); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void willThrowExceptionWhenDeletePostWithInvalidId() { |
| 80 | + // Given |
| 81 | + long postId = 1L; |
| 82 | + given(postRepository.findById(postId)).willReturn(Optional.empty()); |
| 83 | + |
| 84 | + // When |
| 85 | + // Then |
| 86 | + assertThatThrownBy(() -> underTest.deletePost(postId)) |
| 87 | + .isInstanceOf(IllegalArgumentException.class) |
| 88 | + .hasMessageContaining("Post not found"); |
| 89 | + |
| 90 | + verify(postRepository, never()).deleteById(any()); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments