Skip to content
Snippets Groups Projects
Commit c16a683c authored by Chen Liang's avatar Chen Liang
Browse files

Delete LightWeightTest.java

parent 7fe70125
No related branches found
No related tags found
No related merge requests found
package com.cardiff.lcTest;
import com.cardiff.client_project.controller.nursinghome.NursingHomeController;
import com.cardiff.client_project.pojo.dto.HospitalDTO;
import com.cardiff.client_project.service.NursingHomeService;
import com.cardiff.client_project.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Collections;
import java.util.List;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(NursingHomeController.class)
@ExtendWith(SpringExtension.class)
public class LightWeightTest {
//@Autowired:这两个注解使得Spring自动注入MockMvc和ObjectMapper对象。
// MockMvc用于模拟HTTP请求和响应,而ObjectMapper用于在JSON和Java对象之间进行转换。
//
//@MockBean:该注解是Spring Boot的特有注解,用于创建一个虚拟的(mock)NursingHomeService对象,并注入到Controller中。
// 通过这种方式,Service层的逻辑被“mock”掉了,测试可以专注于Controller的行为,而无需依赖真实的Service层逻辑。
@Autowired
private MockMvc mockMvc;
@MockBean
private NursingHomeService nursingHomeService;
@Autowired
private ObjectMapper objectMapper;
//测试:获取可用床位
@Test
public void testGetAvailableBeds() throws Exception {
List<HospitalDTO> mockList = Collections.singletonList(new HospitalDTO());
when(nursingHomeService.getAvailableBeds()).thenReturn(Result.success(mockList));
mockMvc.perform(get("/api/hospitals/available"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:添加医院信息
@Test
public void testAddHospital() throws Exception {
HospitalDTO hospitalDTO = new HospitalDTO();
hospitalDTO.setName("Test Hospital");
when(nursingHomeService.insertPatientInfo(any(HospitalDTO.class)))
.thenReturn(Result.success(hospitalDTO));
mockMvc.perform(post("/api/hospitals")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(hospitalDTO)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:更新床位数
@Test
public void testUpdateBedCount() throws Exception {
when(nursingHomeService.updateBedCount(1, 20)).thenReturn(Result.success("Updated"));
mockMvc.perform(put("/api/hospitals/1/beds")
.param("currentPatients", "20"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:删除医院信息
@Test
public void testDeleteHospital() throws Exception {
when(nursingHomeService.deletePatientById(List.of(1)))
.thenReturn(Result.success("Deleted"));
mockMvc.perform(delete("/api/hospitals/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:搜索医院(带参数)
@Test
public void testSearchHospitalsWithName() throws Exception {
when(nursingHomeService.searchHospitals("Test"))
.thenReturn(Result.success(List.of(new HospitalDTO())));
mockMvc.perform(get("/api/hospitals/search")
.param("name", "Test"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:搜索医院(无参数,返回全部)
@Test
public void testSearchHospitalsWithoutName() throws Exception {
when(nursingHomeService.getAvailableBeds())
.thenReturn(Result.success(List.of(new HospitalDTO())));
mockMvc.perform(get("/api/hospitals/search"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:更新审批状态
@Test
public void testUpdateApprovalStatus() throws Exception {
when(nursingHomeService.updateApprovalStatus(1, "approved"))
.thenReturn(Result.success("Approved"));
mockMvc.perform(put("/api/hospitals/1/approval")
.param("status", "approved"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:获取待审批医院
@Test
public void testGetPendingApprovals() throws Exception {
when(nursingHomeService.getPendingApprovals())
.thenReturn(Result.success(List.of(new HospitalDTO())));
mockMvc.perform(get("/api/hospitals/pending"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment