用户和商家类Controller层测试通过
This commit is contained in:
parent
b211f6ebe5
commit
308241d5d2
@ -2,22 +2,28 @@ package com.example.springdemo;
|
||||
|
||||
import com.example.springdemo.controller.MerchantsController;
|
||||
import com.example.springdemo.controller.UsersController;
|
||||
import com.example.springdemo.entities.Merchants;
|
||||
import com.example.springdemo.entities.Users;
|
||||
import com.example.springdemo.entities.password.MerchantsPassword;
|
||||
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.jetbrains.annotations.NotNull;
|
||||
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.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@WebMvcTest(UsersController.class)
|
||||
class UsersControllerTests {
|
||||
@Resource
|
||||
@ -39,8 +45,11 @@ class UsersControllerTests {
|
||||
|
||||
@Test
|
||||
public void testGetAllUsers() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/users/find/all"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk());
|
||||
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/users/find/all"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
Logger logger = Logger.getLogger("testGetAllUsers");
|
||||
logger.info(result.getResponse().getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -61,16 +70,7 @@ class UsersControllerTests {
|
||||
@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);
|
||||
UsersController.wrapperUserAndPassword usersAndPassword = getWrapperUserAndPassword();
|
||||
|
||||
// Convert to JSON
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
@ -85,6 +85,23 @@ class UsersControllerTests {
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private UsersController.wrapperUserAndPassword getWrapperUserAndPassword() {
|
||||
Users user = new Users();
|
||||
user.setName("test");
|
||||
user.setSex("男");
|
||||
user.setPhoneNumber("12345678901");
|
||||
user.setAddress("test address");
|
||||
UsersPassword userPassword = new UsersPassword();
|
||||
userPassword.setPassword("test1234567");
|
||||
userPassword.setUsers(user);
|
||||
|
||||
UsersController.wrapperUserAndPassword usersAndPassword = new UsersController.wrapperUserAndPassword();
|
||||
usersAndPassword.setUser(user);
|
||||
usersAndPassword.setUserPassword(userPassword);
|
||||
return usersAndPassword;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
public void testDeleteUserById() throws Exception {
|
||||
@ -147,6 +164,9 @@ class MerchantsControllerTests {
|
||||
@MockBean
|
||||
private MerchantsService merchantsService;
|
||||
|
||||
@Resource
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
// Test that the controller loads
|
||||
@ -154,4 +174,107 @@ class MerchantsControllerTests {
|
||||
// Test that the service loads
|
||||
assert merchantsService != null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllMerchants() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/merchants/find/all"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMerchantByName() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/merchants/find/name")
|
||||
.param("name", "软件学院"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMerchantById() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/merchants/find/id")
|
||||
.param("id", "2"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddMerchant() throws Exception {
|
||||
// Create a merchant and password
|
||||
MerchantsController.wrapperMerchantAndPassword merchantAndPassword = getWrapperMerchantAndPassword();
|
||||
|
||||
// Convert to JSON
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
|
||||
|
||||
String jsonString = writer.writeValueAsString(merchantAndPassword);
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/merchants/add")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(jsonString))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private MerchantsController.wrapperMerchantAndPassword getWrapperMerchantAndPassword() {
|
||||
Merchants merchant = new Merchants();
|
||||
merchant.setName("test");
|
||||
merchant.setAddress("test address");
|
||||
merchant.setPhoneNumber("12345678901");
|
||||
merchant.setDescription("test description");
|
||||
|
||||
MerchantsPassword merchantPassword = new MerchantsPassword();
|
||||
merchantPassword.setMerchants(merchant);
|
||||
merchantPassword.setPassword("test1234567");
|
||||
|
||||
MerchantsController.wrapperMerchantAndPassword merchantAndPassword = new MerchantsController.wrapperMerchantAndPassword();
|
||||
merchantAndPassword.setMerchant(merchant);
|
||||
merchantAndPassword.setMerchantPassword(merchantPassword);
|
||||
return merchantAndPassword;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
public void testDeleteMerchantById() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.delete("/merchants/delete/id")
|
||||
.param("id", "2"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
public void testUpdateMerchant() throws Exception {
|
||||
// Create a merchant and password
|
||||
Merchants merchant = new Merchants();
|
||||
merchant.setId(2L);
|
||||
merchant.setName("test");
|
||||
merchant.setAddress("test address");
|
||||
merchant.setPhoneNumber("12345678901");
|
||||
merchant.setDescription("test description");
|
||||
|
||||
// Convert to JSON
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
|
||||
|
||||
String jsonString = writer.writeValueAsString(merchant);
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.put("/merchants/update/info")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(jsonString))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
public void testUpdateMerchantPassword() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.put("/merchants/update/password")
|
||||
.param("password", "test1234567")
|
||||
.param("merchantId", "2"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user