Spaces:
Sleeping
Sleeping
| package com.krishna.controller; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.krishna.dto.CommentDTO; | |
| import com.krishna.model.CommentModel; | |
| import com.krishna.service.CommentService; | |
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.Test; | |
| import org.mockito.Mockito; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | |
| import org.springframework.boot.test.mock.mockito.MockBean; | |
| import org.springframework.http.MediaType; | |
| import org.springframework.test.web.servlet.MockMvc; | |
| import java.util.List; | |
| import java.util.UUID; | |
| import static org.mockito.ArgumentMatchers.any; | |
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | |
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | |
| public class CommentControllerTest { | |
| private MockMvc mockMvc; | |
| private CommentService commentService; | |
| private ObjectMapper objectMapper; | |
| private CommentModel mockComment; | |
| void setup() { | |
| mockComment = new CommentModel(); | |
| mockComment.setId(1L); | |
| mockComment.setContent("Test comment"); | |
| // Optional: You can mock user/post if needed in your model | |
| } | |
| void testAddComment() throws Exception { | |
| CommentDTO dto = new CommentDTO(); | |
| dto.setContent("Test comment"); | |
| dto.setPostId(100L); | |
| dto.setUserId(UUID.randomUUID()); | |
| Mockito.when(commentService.addComment(any(CommentDTO.class))).thenReturn(mockComment); | |
| mockMvc.perform(post("/comments/add") | |
| .contentType(MediaType.APPLICATION_JSON) | |
| .content(objectMapper.writeValueAsString(dto))) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$.content").value("Test comment")); | |
| } | |
| void testGetCommentsByPost() throws Exception { | |
| Mockito.when(commentService.getCommentsByPost(100L)).thenReturn(List.of(mockComment)); | |
| mockMvc.perform(get("/comments/post/100")) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$[0].content").value("Test comment")); | |
| } | |
| void testDeletePost() throws Exception { | |
| Mockito.doNothing().when(commentService).deleteComment(100L); | |
| mockMvc.perform(delete("/comments/100")) | |
| .andExpect(status().isOk()) | |
| .andExpect(content().string("Comment deleted successfully.")); | |
| } | |
| } | |