11package com .capturecat .core .service .tag ;
22
3- import static org .junit .jupiter .api .Assertions .assertThrows ;
4- import static org .mockito .Mockito .mock ;
3+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
4+ import static org .mockito .ArgumentMatchers .anyLong ;
5+ import static org .mockito .ArgumentMatchers .anyString ;
6+ import static org .mockito .BDDMockito .given ;
57import static org .mockito .Mockito .times ;
68import static org .mockito .Mockito .verify ;
79import static org .mockito .Mockito .verifyNoInteractions ;
8- import static org .mockito .Mockito .when ;
910
1011import java .util .Optional ;
1112
1516import org .mockito .Mock ;
1617import org .mockito .junit .jupiter .MockitoExtension ;
1718
19+ import com .capturecat .core .DummyObject ;
1820import com .capturecat .core .domain .tag .ImageTagRepository ;
1921import com .capturecat .core .domain .tag .Tag ;
22+ import com .capturecat .core .domain .tag .TagFixture ;
2023import com .capturecat .core .domain .tag .TagRepository ;
2124import com .capturecat .core .domain .user .User ;
2225import com .capturecat .core .domain .user .UserRepository ;
@@ -41,47 +44,43 @@ class ImageTagServiceTest {
4144 @ Test
4245 void 업데이트_사용자_존재하면_레포지토리_호출한다 () {
4346 // given
44- LoginUser loginUser = mock (LoginUser .class );
45- when (loginUser .getUsername ()).thenReturn ("existingUser" );
47+ User user = DummyObject .newMockUser (123L );
4648
47- User user = mock (User .class );
48- when (user .getId ()).thenReturn (123L );
49-
50- when (userRepository .findByUsername ("existingUser" )).thenReturn (Optional .of (user ));
49+ given (userRepository .findByUsername (anyString ())).willReturn (Optional .of (user ));
5150
5251 // when
53- imageTagService .update (loginUser , 10L , 20L );
52+ imageTagService .update (new LoginUser ( user ) , 10L , 20L );
5453
5554 // then
56- verify (imageTagRepository , times (1 )).updateImageTagsForUser (123L , 10L , 20L );
55+ verify (imageTagRepository , times (1 ))
56+ .updateImageTagsForUser (user .getId (), 10L , 20L );
5757 }
5858
5959 @ Test
6060 void 업데이트_사용자_없으면_CoreException_던진다 () {
6161 // given
62- LoginUser loginUser = mock ( LoginUser . class );
63- when ( loginUser . getUsername ()). thenReturn ( "noUser" );
64- when (userRepository .findByUsername ("noUser" )). thenReturn (Optional .empty ());
62+ User user = DummyObject . newUser ( "noUser" );
63+
64+ given (userRepository .findByUsername (anyString ())). willReturn (Optional .empty ());
6565
66- // when / then
67- assertThrows (CoreException .class , () -> imageTagService .update (loginUser , 1L , 2L ));
66+ // when & then
67+ assertThatThrownBy (() -> imageTagService .update (new LoginUser (user ), 1L , 2L ))
68+ .isInstanceOf (CoreException .class );
6869 verifyNoInteractions (imageTagRepository );
6970 }
7071
7172 @ Test
7273 void 삭제_사용자_태그_존재하면_레포지토리_호출한다 () {
7374 // given
74- LoginUser loginUser = mock (LoginUser .class );
75- when (loginUser .getUsername ()).thenReturn ("existingUser" );
75+ User user = DummyObject .newUser ("test" );
7676
77- User user = mock (User .class );
78- when (userRepository .findByUsername ("existingUser" )).thenReturn (Optional .of (user ));
77+ given (userRepository .findByUsername (anyString ())).willReturn (Optional .of (user ));
7978
80- Tag tag = mock ( Tag . class );
81- when (tagRepository .findById (10L )). thenReturn (Optional .of (tag ));
79+ Tag tag = TagFixture . createTag ( 10L , "testTag" );
80+ given (tagRepository .findById (anyLong ())). willReturn (Optional .of (tag ));
8281
8382 // when
84- imageTagService .delete (loginUser , 10L );
83+ imageTagService .delete (new LoginUser ( user ), tag . getId () );
8584
8685 // then
8786 verify (imageTagRepository , times (1 )).deleteByTagAndUser (tag , user );
@@ -90,28 +89,27 @@ class ImageTagServiceTest {
9089 @ Test
9190 void 삭제_사용자_없으면_CoreException_던진다 () {
9291 // given
93- LoginUser loginUser = mock (LoginUser .class );
94- when (loginUser .getUsername ()).thenReturn ("noUser" );
95- when (userRepository .findByUsername ("noUser" )).thenReturn (Optional .empty ());
92+ User user = DummyObject .newUser ("noUser" );
9693
97- // when / then
98- assertThrows (CoreException .class , () -> imageTagService .delete (loginUser , 1L ));
94+ given (userRepository .findByUsername (anyString ())).willReturn (Optional .empty ());
95+
96+ // when & then
97+ assertThatThrownBy (() -> imageTagService .delete (new LoginUser (user ), 1L ))
98+ .isInstanceOf (CoreException .class );
9999 verifyNoInteractions (imageTagRepository , tagRepository );
100100 }
101101
102102 @ Test
103103 void 삭제_태그_없으면_CoreException_던진다 () {
104104 // given
105- LoginUser loginUser = mock (LoginUser .class );
106- when (loginUser .getUsername ()).thenReturn ("existingUser" );
107-
108- User user = mock (User .class );
109- when (userRepository .findByUsername ("existingUser" )).thenReturn (Optional .of (user ));
105+ User user = DummyObject .newUser ("test" );
110106
111- when (tagRepository .findById (2L )).thenReturn (Optional .empty ());
107+ given (userRepository .findByUsername (anyString ())).willReturn (Optional .of (user ));
108+ given (tagRepository .findById (anyLong ())).willReturn (Optional .empty ());
112109
113- // when / then
114- assertThrows (CoreException .class , () -> imageTagService .delete (loginUser , 2L ));
110+ // when & then
111+ assertThatThrownBy (() -> imageTagService .delete (new LoginUser (user ), 2L ))
112+ .isInstanceOf (CoreException .class );
115113 verifyNoInteractions (imageTagRepository );
116114 }
117115}
0 commit comments