用户和商家类Controller层测试通过

This commit is contained in:
myh 2023-11-14 15:32:16 +08:00
parent b211f6ebe5
commit 308241d5d2

View File

@ -2,22 +2,28 @@ package com.example.springdemo;
import com.example.springdemo.controller.MerchantsController; import com.example.springdemo.controller.MerchantsController;
import com.example.springdemo.controller.UsersController; import com.example.springdemo.controller.UsersController;
import com.example.springdemo.entities.Merchants;
import com.example.springdemo.entities.Users; import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.MerchantsPassword;
import com.example.springdemo.entities.password.UsersPassword; import com.example.springdemo.entities.password.UsersPassword;
import com.example.springdemo.service.MerchantsService; import com.example.springdemo.service.MerchantsService;
import com.example.springdemo.service.UsersService; import com.example.springdemo.service.UsersService;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.annotation.Rollback; import org.springframework.test.annotation.Rollback;
import org.springframework.test.web.servlet.MockMvc; 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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import java.util.logging.Logger;
@WebMvcTest(UsersController.class) @WebMvcTest(UsersController.class)
class UsersControllerTests { class UsersControllerTests {
@Resource @Resource
@ -39,8 +45,11 @@ class UsersControllerTests {
@Test @Test
public void testGetAllUsers() throws Exception { public void testGetAllUsers() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users/find/all")) MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/users/find/all"))
.andExpect(MockMvcResultMatchers.status().isOk()); .andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
Logger logger = Logger.getLogger("testGetAllUsers");
logger.info(result.getResponse().getContentAsString());
} }
@Test @Test
@ -61,16 +70,7 @@ class UsersControllerTests {
@Rollback(value = true) @Rollback(value = true)
public void testAddUser() throws Exception { public void testAddUser() throws Exception {
// Create a user and password // Create a user and password
Users user = new Users(); UsersController.wrapperUserAndPassword usersAndPassword = getWrapperUserAndPassword();
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 // Convert to JSON
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@ -85,6 +85,23 @@ class UsersControllerTests {
.andReturn(); .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 @Test
@Rollback(value = true) @Rollback(value = true)
public void testDeleteUserById() throws Exception { public void testDeleteUserById() throws Exception {
@ -147,6 +164,9 @@ class MerchantsControllerTests {
@MockBean @MockBean
private MerchantsService merchantsService; private MerchantsService merchantsService;
@Resource
private MockMvc mockMvc;
@Test @Test
public void contextLoads() { public void contextLoads() {
// Test that the controller loads // Test that the controller loads
@ -154,4 +174,107 @@ class MerchantsControllerTests {
// Test that the service loads // Test that the service loads
assert merchantsService != null; 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();
}
} }