完善框架结构,解决了validation和saToken的问题
This commit is contained in:
@@ -1,34 +1,70 @@
|
||||
package com.guo.learningprogresstracker.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.guo.learningprogresstracker.dto.request.TaskRequest;
|
||||
import com.guo.learningprogresstracker.dto.response.TaskInfoResponse;
|
||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.service.impl.TasksServiceImpl;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
class TaskControllerTest {
|
||||
|
||||
@Autowired
|
||||
private TaskController taskController;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper jacksonObjectMapper;
|
||||
@Autowired
|
||||
private TasksServiceImpl tasksServiceImpl;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StpUtil.login("测试用户", "test_driver");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addTask() {
|
||||
void addTask() throws Exception {
|
||||
TaskRequest taskRequest = getTaskRequest();
|
||||
|
||||
String s = jacksonObjectMapper.writeValueAsString(taskRequest);
|
||||
System.out.println("Token Value: " + StpUtil.getTokenValue());
|
||||
mockMvc.perform(post("/tasks")
|
||||
.header("satoken", StpUtil.getTokenValue())
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(s))
|
||||
.andDo(print())
|
||||
.andExpect(jsonPath("$.code").value(200));
|
||||
|
||||
}
|
||||
|
||||
private static TaskRequest getTaskRequest() {
|
||||
TaskRequest taskRequest = mock(TaskRequest.class);
|
||||
|
||||
when(taskRequest.getId()).thenReturn(null);
|
||||
// 设置学习任务的名称
|
||||
when(taskRequest.getTaskName()).thenReturn("测试任务名称");
|
||||
|
||||
@@ -45,37 +81,57 @@ class TaskControllerTest {
|
||||
when(taskRequest.getContentDifficulty()).thenReturn(4);
|
||||
|
||||
// 设置任务的未来价值
|
||||
when(taskRequest.getFutureValue()).thenReturn(5);
|
||||
when(taskRequest.getFutureValue()).thenReturn(4);
|
||||
|
||||
// 设置用户对任务的主观优先级
|
||||
when(taskRequest.getSubjectivePriority()).thenReturn(2);
|
||||
CommonResult commonResult = taskController.addTask(taskRequest);
|
||||
|
||||
// 验证 addTask 是否被调用一次
|
||||
assertEquals(commonResult.getCode(), 200,"测试数据插入失败");
|
||||
|
||||
when(taskRequest.getSubjectivePriority()).thenReturn(1);
|
||||
return taskRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试获取全部数据
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
void showTask() {
|
||||
String taskId = mock(String.class);
|
||||
TaskInfoResponse mockData = mock(TaskInfoResponse.class);
|
||||
void getTask() throws Exception {
|
||||
String taskId = "1";
|
||||
mockMvc.perform(get("/tasks/"+taskId)
|
||||
.header("satoken",StpUtil.getTokenValue()))
|
||||
.andDo(print())
|
||||
.andExpect(jsonPath("$.code").value(200))
|
||||
.andExpect(jsonPath("$.data").exists());
|
||||
|
||||
CommonResult commonResult = taskController.showTask(taskId);
|
||||
assertEquals(commonResult.getCode(),200,"获取数据失败");
|
||||
assertNotNull(commonResult.getData(),"返回值中不存在数据");
|
||||
assertEquals(commonResult.getData(),mockData,"返回值携带数据不正确");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTask() {
|
||||
|
||||
TaskRequest taskRequest = mock(TaskRequest.class);
|
||||
when(taskRequest.getTaskName()).thenReturn("测试数据名称");
|
||||
when(taskRequest.getMaterialUrl()).thenReturn("http://example.com/material");
|
||||
CommonResult commonResult = taskController.updateTask("0", taskRequest);
|
||||
assertEquals(commonResult.getData(), 200, "任务信息更新失败");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTask() {
|
||||
void deleteTask() throws Exception {
|
||||
CommonResult commonResult = taskController.addTask(getTaskRequest());
|
||||
assertEquals(ObjectUtils.isNotEmpty(commonResult.getMessage()),"任务创建失败");
|
||||
TaskEntity taskEntity = tasksServiceImpl.getOne(Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, commonResult.getMessage()));
|
||||
assertNotNull(taskEntity,"未能找到新创建的任务实体");
|
||||
mockMvc.perform(delete("/tasks/"+taskEntity.getId()))
|
||||
.andDo(print())
|
||||
.andExpect(jsonPath("$.code").value(200))
|
||||
.andExpect(jsonPath("$.data").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTasks() {
|
||||
void listTasks() throws Exception {
|
||||
mockMvc.perform(get("/tasks")
|
||||
.header("satoken",StpUtil.getTokenValue()))
|
||||
.andDo(print())
|
||||
.andExpect(jsonPath("$.code").value(200))
|
||||
.andExpect(jsonPath("$.data").exists());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user