UsersController 测试

This commit is contained in:
myh 2023-11-12 19:35:27 +08:00
parent 1f8754ef4c
commit d2eb90df7b
2 changed files with 114 additions and 7 deletions

View File

@ -2,12 +2,21 @@ package com.example.springdemo;
import com.example.springdemo.controller.MerchantsController;
import com.example.springdemo.controller.UsersController;
import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword;
import com.example.springdemo.service.MerchantsService;
import com.example.springdemo.service.UsersService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
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.annotation.Rollback;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@WebMvcTest(UsersController.class)
class UsersControllerTests {
@ -17,6 +26,9 @@ class UsersControllerTests {
@MockBean
private UsersService usersService;
@Resource
private MockMvc mockMvc;
@Test
public void contextLoads() {
// Test that the controller loads
@ -24,6 +36,107 @@ class UsersControllerTests {
// Test that the service loads
assert usersService != null;
}
@Test
public void testGetAllUsers() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users/find/all"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void testGetUserByName() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users/find/name").param("name", "admin"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
@Test
public void testGetUserById() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users/find/id").param("id", "1"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
@Test
@Rollback(value = true)
public void testAddUser() throws Exception {
// Create a user and password
Users user = new Users();
user.setName("test");
user.setSex("");
UsersPassword userPassword = new UsersPassword();
userPassword.setPassword("test1234567");
userPassword.setUsers(user);
UsersController.wrapperUserAndPassword usersAndPassword = new UsersController.wrapperUserAndPassword();
usersAndPassword.setUser(user);
usersAndPassword.setUserPassword(userPassword);
// Convert to JSON
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
String jsonString = writer.writeValueAsString(usersAndPassword);
mockMvc.perform(MockMvcRequestBuilders.post("/users/add")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonString))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
@Test
@Rollback(value = true)
public void testDeleteUserById() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/users/delete/id").param("id", "2"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
@Test
@Rollback(value = true)
public void testDeleteUserByName() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/users/delete/name").param("name", "测试用户"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
@Test
@Rollback(value = true)
public void testUpdateUser() throws Exception {
// Create a user and password
Users user = new Users();
user.setId(2L);
user.setName("test");
user.setSex("");
user.setPhoneNumber("12345678901");
user.setAddress("test address");
UsersPassword userPassword = new UsersPassword();
userPassword.setPassword("test1234567");
userPassword.setUsers(user);
// Convert to JSON
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
String jsonString = writer.writeValueAsString(user);
mockMvc.perform(MockMvcRequestBuilders.put("/users/update/info")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonString))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
@Test
@Rollback(value = true)
public void testUpdateUserPassword() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.put("/users/update/password")
.param("password", "test1234567")
.param("userId", "2"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
}
@WebMvcTest(MerchantsController.class)
@ -41,4 +154,4 @@ class MerchantsControllerTests {
// Test that the service loads
assert merchantsService != null;
}
}
}

View File

@ -6,13 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringDemoApplicationTests {
@Test
void test(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
@Test
void contextLoads() {
}
}