Last active
July 27, 2021 12:56
-
-
Save babjo/d2ed4b69745b0cf025d1b0dac4bf2f3f to your computer and use it in GitHub Desktop.
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
public class BlogServiceTest { | |
@Mock | |
private AuthClient authClient; // mock 객체생성 | |
@Mock | |
private BlogRepository blogRepository; // mock 객체생성 | |
@Test | |
public void addPost() { | |
// given | |
given(authClient.verifyToken(anyString())).willReturn(1L); | |
given(blogRepository.save(any())).willThrow(new Post(1L, "test")); | |
BlogService service = new BlogService(authClient, blogRepository); | |
String token = "testToken"; | |
Post post = new Post("test"); | |
// when | |
service.addPost(new AddPostRequest(token, post)); | |
// then | |
verify(authClient).should().verifyToken(anyString()); // 행위검증 | |
verify(blogRepository).should().save(any()); // 행위검증 | |
} | |
@Test | |
public void addPost_thrownByAuthClient() { | |
// given | |
given(authClient.verifyToken(any())).willThrow(new RuntimeException()); | |
BlogService service = new BlogService(authClient, blogRepository); | |
String token = "testToken"; | |
Post post = new Post("test"); | |
// when | |
assertThrows(RuntimeException.class, () -> { | |
service.addPost(new AddPostRequest(token, post)); | |
}); | |
// then | |
verify(authClient).should().verifyToken(any()); // 행위검증 | |
verifyZeroInteractions(blogRepository); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment