From 308241d5d2751d8acf183cb8a710e3a4f1b1360b Mon Sep 17 00:00:00 2001 From: myh Date: Tue, 14 Nov 2023 15:32:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=92=8C=E5=95=86=E5=AE=B6?= =?UTF-8?q?=E7=B1=BBController=E5=B1=82=E6=B5=8B=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/springdemo/ControllerTests.java | 147 ++++++++++++++++-- 1 file changed, 135 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/example/springdemo/ControllerTests.java b/src/test/java/com/example/springdemo/ControllerTests.java index fdbf34a..0c5720d 100644 --- a/src/test/java/com/example/springdemo/ControllerTests.java +++ b/src/test/java/com/example/springdemo/ControllerTests.java @@ -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(); + } + } \ No newline at end of file