π 2024λ 01μ 25μΌ
μ°κ΄ κ΄κ³μμ λ°μνλ μ΄μλ‘ μ°κ΄ κ΄κ³κ° μ€μ λ μν°ν°λ₯Ό μ‘°νν κ²½μ°μ μ‘°νλ λ°μ΄ν° κ°―μ(n) λ§νΌ μ°κ΄κ΄κ³μ μ‘°ν μΏΌλ¦¬κ° μΆκ°λ‘ λ°μνμ¬ λ°μ΄ν°λ₯Ό μ½μ΄μ€κ² λλ€.
(JPA Repositoryλ‘ find μ μ€ννλ 첫 쿼리μμ νμ μν°ν°κΉμ§ ν λ²μ κ°μ Έμ€μ§ μκ³ , νμ μν°ν°λ₯Ό μ¬μ©ν λ μΆκ°λ‘ μ‘°ννκΈ° λλ¬Έ)
- PostRepository.java
@Query("select p from Post p left outer join fetch p.author") List<Post> findAllFetchJoin();
- AthorControllerTest
/* @WebMvcTest(AuthorController.class) λ₯Ό μ΄μ©νμ¬ Controller κ³μΈ΅μ ν μ€νΈμ ν μ€νΈ λͺ¨λ μ€νλ§ λΉμ μμ±νκ³ μ£Όμ νμ§λ μλλ€. */ @SpringBootTest @AutoConfigureMockMvc public class AuthorControllerTest { @Autowired private MockMvc mockMvc; @MockBean public AuthorService authorService; @Test // @WithMockUser security μμ‘΄μ± μΆκ° νμ void authorDetailTest() throws Exception { AuthorDetailResDto authorDetailResDto = new AuthorDetailResDto(); authorDetailResDto.setName("test"); authorDetailResDto.setEmail("test@naver.com"); authorDetailResDto.setEmail("sadqwexdq"); Mockito.when(authorService.findAuthorDetail(1L)).thenReturn(authorDetailResDto); mockMvc.perform(MockMvcRequestBuilders.get("/author/30/circle/dto")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect((ResultMatcher) jsonPath("$.name",authorDetailResDto.getName())); } }
- AuthorRepositoryTest
/* @DataJpaTest λ₯Ό μ¬μ©νλ©΄ 맀 ν μ€νΈκ° μ’ λ£λλ©΄ μλμΌλ‘ DB μμ볡ꡬ λͺ¨λ μ€νλ§ λΉμ μμ±νμ§λ μκ³ DB ν μ€νΈ νΉν μ΄λ Έν μ΄μ μ΄λ€. @SpringBootTest λ μλ λ‘€λ°± κΈ°λ₯μ μ§μνμ§ μκ³ , λ³λλ‘ λ‘€λ°± μ½λκ° νμ. μ€μ μ€νλ§ μ€νκ³Ό λμΌνκ² μ€νλ§ λΉ μμ± λ° μ£Όμ */ //@DataJpaTest @SpringBootTest //@Transactional /* replace = AutoConfigureTestDatabase.Replace.ANY : H2 db(spring λ΄μ₯) */ @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) public class AuthorRepositoryTest { @Autowired private AuthorRepository authorRepository; @Test public void authorSaveTest(){ // μ€λΉ λ¨κ³ (prepare, given) Author author = Author.builder() .name("λ°μΈμ’ ") .email("μΈμ’ 3@νν") .password("1234") .role(Role.USER) .build(); // μ€ν λ¨κ³ (execute, when) authorRepository.save(author); Author authorDB = authorRepository.findByEmail("μΈμ’ 3@νν").orElse(null); // κ²μ¦ λ¨κ³ (then) assertEquals(author.getEmail(), authorDB.getEmail()); } }
- AuthorServiceTest
@SpringBootTest public class AuthorServiceTest { @Autowired AuthorService authorService; @Autowired PostRepository postRepository; // κ°μ§κ°μ²΄λ₯Ό λ§λλ μμ μ λͺ©νΉμ΄λΌκ³ νλ€ @MockBean private AuthorRepository authorRepository; @Test void findAuthorDetailTest(){ Long authorId = 1L; List<Post> posts = new ArrayList<>(); Post post = Post.builder() .title("hello") .contents("hello word") .build(); posts.add(post); Author author = Author.builder() .id(authorId) .name("test1") .email("test1@naver.com") .password("1234") .posts(posts) .build(); Mockito.when(authorRepository.findById(authorId)).thenReturn(Optional.of(author)); AuthorDetailResDto authorDetailResDto = authorService.findAuthorDetail(authorId); Assertions.assertEquals(author.getName(), authorDetailResDto.getName()); Assertions.assertEquals(author.getPosts().size(), authorDetailResDto.getPostCount()); Assertions.assertEquals("μΌλ°μ μ ", authorDetailResDto.getRole()); } @Test public void updateTest(){ Long author_id = 1L; Author author = Author.builder() .name("test1") .email("test1@naver.com") .password("1234") .build(); Mockito.when(authorRepository.findById(author_id)).thenReturn(Optional.of(author)); AuthorUpdateDto authorUpdateDto = new AuthorUpdateDto(); authorUpdateDto.setName("test2"); authorUpdateDto.setPassword("test2@naver.com"); authorUpdateDto.setId(author_id); Author author_new = authorService.update(authorUpdateDto); Assertions.assertEquals(author_new.getName(), authorUpdateDto.getName()); } @Test public void findAllTest(){ int beforeCount = authorService.findAll().size(); // Mock repository κΈ°λ₯ ꡬν List<Author> authors = new ArrayList<>(); authors.add(new Author()); authors.add(new Author()); Mockito.when(authorRepository.findAll()).thenReturn(authors); // κ²μ¦ Assertions.assertEquals(2, authorService.findAll().size()); } }