Skip to content
Snippets Groups Projects
Commit 89634945 authored by wyl's avatar wyl
Browse files

UPDATE TEST

parent b7de38c6
No related merge requests found
package com.cardiff;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@SpringBootApplication
@EnableWebSecurity
public class HealthCareTestApplication {
public static void main(String[] args) {
SpringApplication.run(HealthCareTestApplication.class, args);
}
}
package com.cardiff.wylTest;
import com.cardiff.client_project.utils.Result;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpTest {
@Value(value="${local.server.port}")
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testFaultPage() throws Exception {
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/fault.html",
String.class)).contains("You do not have sufficient access rights, the page will be redirected to the login page within");
}
@Test
public void testLoginPage() throws Exception {
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/login.html",
String.class)).contains("Verification code");
}
@Test
public void testLogin() throws Exception {
String loginUrl = "http://localhost:" + port + "/superAdmin/sign";
Map<String, String> loginData = new HashMap<>();
loginData.put("name", "2649783657@qq.com");
loginData.put("password", "admin");
loginData.put("role","super");
loginData.put("status","1");
ResponseEntity<Result> loginResponse = this.restTemplate.postForEntity(loginUrl, loginData, Result.class);
assertThat(loginResponse.getStatusCode().value()).isEqualTo(200);
}
}
package com.cardiff.wylTest;
import com.cardiff.client_project.HealthCareApplication;
import com.cardiff.client_project.config.WebSecurityConfig;
import com.cardiff.client_project.controller.admin.SuperAdminController;
import com.cardiff.client_project.pojo.dto.SelectDTO;
import com.cardiff.client_project.pojo.dto.SignUserDTO;
import com.cardiff.client_project.service.SuperAdminService;
import com.cardiff.client_project.utils.Result;
import org.junit.internal.Classes;
import org.junit.jupiter.api.Named;
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.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Arrays;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.naming.Name;
@ExtendWith(SpringExtension.class)
@WebMvcTest(SuperAdminController.class)
public class LightweightTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SuperAdminService superAdminService;
@Autowired
private ObjectMapper objectMapper; // Jackson 用于 JSON 序列化
/**
* Testing the user registration interface
* private String name;
* private String password;
* private String email;
* private String phone;
* private String address;
* private String type;
* private String role;
* private int status;
*/
@WithMockUser(username = "2649783657@qq.com",password = "admin",roles = "super")
@Test
public void testSignIn() throws Exception {
SignUserDTO signUserDTO = new SignUserDTO();
signUserDTO.setName("57@qq.com");
signUserDTO.setPassword("admin");
signUserDTO.setEmail("57657@qq.com");
signUserDTO.setPhone("2649783657@qq.com");
signUserDTO.setAddress("test");
signUserDTO.setType("admin");
signUserDTO.setRole("admin");
signUserDTO.setStatus(1);
Result mockResult = Result.error("User registered error");
when(superAdminService.insertUserInform(any(SignUserDTO.class))).thenReturn(mockResult);
mockMvc.perform(post("/superAdmin/sign")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(signUserDTO)))
.andExpect(status().is(403));
}
/**
* Test the paging query interface
*/
@WithMockUser(username = "2649783657@qq.com",password = "admin")
@Test
public void testPageSelect() throws Exception {
Result mockResult = Result.success("Page select success");
when(superAdminService.selectPage("admin", 10, 1)).thenReturn(mockResult);
mockMvc.perform(get("/superAdmin/pageSelect")
.param("type", "admin")
.param("pageSize", "10")
.param("pageNumber", "1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.msg").value("Page select success"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment