blob: d9f9f8793263819bac12814c8599f6eaa59a9f07 [file] [log] [blame]
package com.g9.g9backend.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.g9.g9backend.pojo.Comment;
import com.g9.g9backend.pojo.DTO.GetCommentDTO;
import com.g9.g9backend.service.CommentService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.http.ResponseEntity;
import java.util.Collections;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.BDDMockito.given;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class CommentControllerTest {
@Mock
private CommentService commentService;
@InjectMocks
private CommentController commentController;
@Test
void testPostComment() {
Comment comment = new Comment();
ResponseEntity<String> response = commentController.postComment(comment);
verify(commentService).save(comment);
assertEquals(200, response.getStatusCode().value());
}
@Test
void testDeleteComment() {
Integer id = 1;
ResponseEntity<String> response = commentController.deleteComment(id);
verify(commentService).removeById(id);
assertEquals(204, response.getStatusCode().value());
}
@Test
void testGetCommentWithResourceType() {
Comment comment = new Comment();
comment.setCommentId(1);
comment.setUserId(2);
comment.setReplyId(null);
comment.setContent("Test");
comment.setCreateAt(new Date());
comment.setResourceId(1);
Page<Comment> page = new Page<>();
page.setRecords(Collections.singletonList(comment));
given(commentService.page(ArgumentMatchers.<Page<Comment>>any(), ArgumentMatchers.<LambdaQueryWrapper<Comment>>any())).willReturn(page);
ResponseEntity<GetCommentDTO> response = commentController.getComment(1, 1, 10, "资源");
assertEquals(200, response.getStatusCode().value());
assertEquals(1, response.getBody().getRecords().size());
}
}