build: 单元测试有提取结果参数
This commit is contained in:
parent
f4fa267d7a
commit
164b58ef63
|
@ -2,10 +2,11 @@ package io.metersphere.project.controller;
|
||||||
|
|
||||||
import io.metersphere.project.domain.Project;
|
import io.metersphere.project.domain.Project;
|
||||||
import io.metersphere.project.service.ProjectService;
|
import io.metersphere.project.service.ProjectService;
|
||||||
|
import io.metersphere.validation.groups.Created;
|
||||||
|
import io.metersphere.validation.groups.Updated;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -19,4 +20,14 @@ public class ProjectController {
|
||||||
public List<Project> selectAll() {
|
public List<Project> selectAll() {
|
||||||
return projectService.list();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
@ -18,4 +19,17 @@ public class ProjectService {
|
||||||
public List<Project> list() {
|
public List<Project> list() {
|
||||||
return projectMapper.selectByExample(new ProjectExample());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
package io.metersphere.project.controller;
|
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 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.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
@ -16,13 +21,51 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
public class ProjectControllerTests {
|
public class ProjectControllerTests {
|
||||||
@Resource
|
@Resource
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
private static String projectId;
|
||||||
|
|
||||||
|
// 添加项目
|
||||||
@Test
|
@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 {
|
public void testSelectAll() throws Exception {
|
||||||
LogUtils.info("testSelectAll");
|
|
||||||
mockMvc.perform(MockMvcRequestBuilders.get("/project/list-all"))
|
mockMvc.perform(MockMvcRequestBuilders.get("/project/list-all"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||||
|
@ -30,12 +73,4 @@ public class ProjectControllerTests {
|
||||||
.andDo(print());
|
.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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue