mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-02 03:58:33 +08:00
build: API添加一个单元测试模版
This commit is contained in:
parent
3aec662775
commit
805682d92b
@ -2,12 +2,11 @@ package io.metersphere.api.controller;
|
||||
|
||||
import io.metersphere.api.dto.ApiDefinitionDTO;
|
||||
import io.metersphere.api.service.ApiDefinitionService;
|
||||
import io.metersphere.validation.groups.Created;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
@ -19,9 +18,9 @@ public class ApiDefinitionController {
|
||||
@Resource
|
||||
private ApiDefinitionService apiDefinitionService;
|
||||
|
||||
@PostMapping(value = "/create", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
|
||||
public ApiDefinitionDTO create(@RequestPart("request") ApiDefinitionDTO request,
|
||||
@RequestPart(value = "files", required = false) List<MultipartFile> bodyFiles) {
|
||||
@PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ApiDefinitionDTO create(@Validated({Created.class}) @RequestBody ApiDefinitionDTO request,
|
||||
@RequestParam(value = "files") List<MultipartFile> bodyFiles) {
|
||||
return apiDefinitionService.create(request, bodyFiles);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package io.metersphere.api.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.ApiDefinitionDTO;
|
||||
import io.metersphere.sdk.util.LogUtils;
|
||||
import io.metersphere.utils.JsonUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.Before;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -16,6 +17,8 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@ -25,24 +28,44 @@ public class ApiDefinitionControllerTests {
|
||||
private MockMvc mockMvc;
|
||||
private final static String prefix = "/api/definition";
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
LogUtils.info("init base api test");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void testCreate() throws Exception {
|
||||
LogUtils.info("create api test");
|
||||
// 创建一个 MockMultipartFile 对象,用于模拟文件上传
|
||||
MockMultipartFile file = new MockMultipartFile("files", "test.txt", "application/octet-stream", "Test content".getBytes());
|
||||
MockMultipartFile file = new MockMultipartFile("files", "files", MediaType.APPLICATION_OCTET_STREAM_VALUE, "Test content".getBytes());
|
||||
|
||||
// 创建一个 ApiDefinitionDTO 对象,用于模拟请求的一部分
|
||||
ApiDefinitionDTO request = new ApiDefinitionDTO();
|
||||
// 补充属性内容
|
||||
request.setId("test-api-id");
|
||||
request.setCreateUser("test-user");
|
||||
request.setCreateTime(System.currentTimeMillis());
|
||||
request.setUpdateUser("test-api-id");
|
||||
request.setUpdateTime(System.currentTimeMillis());
|
||||
request.setProjectId("test-project-id");
|
||||
request.setName("test-api-name");
|
||||
request.setMethod("test-api-method");
|
||||
request.setPath("test-api-path");
|
||||
request.setProtocol("test-api-protocol");
|
||||
request.setPos(1l);
|
||||
request.setLatest(true);
|
||||
request.setSyncEnable(true);
|
||||
request.setStatus("test-api-status");
|
||||
request.setVersionId("test-api-version");
|
||||
request.setDeleted(false);
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/create")
|
||||
mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/add")
|
||||
.file(file)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.param("request", new ObjectMapper().writeValueAsString(request)))
|
||||
.andExpect(MockMvcResultMatchers.status().is(400));
|
||||
//.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
||||
//.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("test-api-id"));
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.content(JsonUtils.toJSONString(request)))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.data.id").value("test-api-id"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user