diff --git a/src/test/java/com/example/springdemo/controller/MerchantsControllerTests.java b/src/test/java/com/example/springdemo/controller/MerchantsControllerTests.java new file mode 100644 index 0000000..05fc773 --- /dev/null +++ b/src/test/java/com/example/springdemo/controller/MerchantsControllerTests.java @@ -0,0 +1,140 @@ +package com.example.springdemo.controller; + +import com.example.springdemo.entities.Merchants; +import com.example.springdemo.entities.password.MerchantsPassword; +import com.example.springdemo.service.MerchantsService; +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.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +@WebMvcTest(MerchantsController.class) +class MerchantsControllerTests { + @Resource + private MerchantsController merchantsController; + + @MockBean + private MerchantsService merchantsService; + + @Resource + private MockMvc mockMvc; + + @Test + public void contextLoads() { + // Test that the controller loads + assert merchantsController != null; + // 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(); + } + +} diff --git a/src/test/java/com/example/springdemo/ControllerTests.java b/src/test/java/com/example/springdemo/controller/UsersControllerTests.java similarity index 53% rename from src/test/java/com/example/springdemo/ControllerTests.java rename to src/test/java/com/example/springdemo/controller/UsersControllerTests.java index 0c5720d..3d26161 100644 --- a/src/test/java/com/example/springdemo/ControllerTests.java +++ b/src/test/java/com/example/springdemo/controller/UsersControllerTests.java @@ -1,17 +1,13 @@ -package com.example.springdemo; +package com.example.springdemo.controller; -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.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -35,6 +31,11 @@ class UsersControllerTests { @Resource private MockMvc mockMvc; + @BeforeEach + public void setup() { + + } + @Test public void contextLoads() { // Test that the controller loads @@ -70,7 +71,7 @@ class UsersControllerTests { @Rollback(value = true) public void testAddUser() throws Exception { // Create a user and password - UsersController.wrapperUserAndPassword usersAndPassword = getWrapperUserAndPassword(); + UsersController.wrapperUserAndPassword usersAndPassword = setWrapperUserAndPassword(); // Convert to JSON ObjectMapper mapper = new ObjectMapper(); @@ -86,7 +87,7 @@ class UsersControllerTests { } @NotNull - private UsersController.wrapperUserAndPassword getWrapperUserAndPassword() { + private UsersController.wrapperUserAndPassword setWrapperUserAndPassword() { Users user = new Users(); user.setName("test"); user.setSex("男"); @@ -155,126 +156,3 @@ class UsersControllerTests { .andReturn(); } } - -@WebMvcTest(MerchantsController.class) -class MerchantsControllerTests { - @Resource - private MerchantsController merchantsController; - - @MockBean - private MerchantsService merchantsService; - - @Resource - private MockMvc mockMvc; - - @Test - public void contextLoads() { - // Test that the controller loads - assert merchantsController != null; - // 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