build: 单元测试有提取结果参数

This commit is contained in:
CaptainB 2023-05-26 18:47:37 +08:00 committed by 刘瑞斌
parent f4fa267d7a
commit 164b58ef63
3 changed files with 73 additions and 13 deletions

View File

@ -2,10 +2,11 @@ package io.metersphere.project.controller;
import io.metersphere.project.domain.Project;
import io.metersphere.project.service.ProjectService;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -19,4 +20,14 @@ public class ProjectController {
public List<Project> selectAll() {
return projectService.list();
}
@PostMapping("add")
public Project add(@Validated({Created.class}) @RequestBody Project project) {
return projectService.add(project);
}
@PostMapping("edit")
public Project edit(@Validated({Updated.class}) @RequestBody Project project) {
return projectService.edit(project);
}
}

View File

@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.UUID;
@Service
@Transactional(rollbackFor = Exception.class)
@ -18,4 +19,17 @@ public class ProjectService {
public List<Project> list() {
return projectMapper.selectByExample(new ProjectExample());
}
public Project add(Project project) {
project.setId(UUID.randomUUID().toString());
project.setCreateTime(System.currentTimeMillis());
project.setUpdateTime(System.currentTimeMillis());
projectMapper.insertSelective(project);
return project;
}
public Project edit(Project project) {
projectMapper.updateByPrimaryKeySelective(project);
return projectMapper.selectByPrimaryKey(project.getId());
}
}

View File

@ -1,8 +1,13 @@
package io.metersphere.project.controller;
import io.metersphere.sdk.util.LogUtils;
import com.jayway.jsonpath.JsonPath;
import io.metersphere.project.domain.Project;
import io.metersphere.sdk.util.JSON;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
@ -16,13 +21,51 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ProjectControllerTests {
@Resource
private MockMvc mockMvc;
private static String projectId;
// 添加项目
@Test
@Order(1)
public void testAddProject() throws Exception {
Project project = new Project();
project.setName("test");
project.setCreateUser("admin");
project.setWorkspaceId("default");
var result = mockMvc.perform(MockMvcRequestBuilders.post("/project/add")
.content(JSON.toJSONString(project))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
projectId = JsonPath.read(result.getResponse().getContentAsString(), "$.data.id");
}
@Test
@Order(2)
public void testEditProject() throws Exception {
Project project = new Project();
project.setId(projectId);
project.setName("test2");
project.setCreateUser("admin");
project.setWorkspaceId("default");
mockMvc.perform(MockMvcRequestBuilders.post("/project/edit")
.content(JSON.toJSONString(project))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andDo(print());
}
@Test
@Order(3)
public void testSelectAll() throws Exception {
LogUtils.info("testSelectAll");
mockMvc.perform(MockMvcRequestBuilders.get("/project/list-all"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
@ -30,12 +73,4 @@ public class ProjectControllerTests {
.andDo(print());
}
@Test
public void testSelectAll2() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/project/list-all"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
// .andExpect(jsonPath("$.person.name").value("Jason"))
.andDo(print());
}
}