测试代码重构

This commit is contained in:
myh 2023-11-15 17:32:54 +08:00
parent 3b6b016091
commit 3c3f2a8cc5
2 changed files with 149 additions and 131 deletions

View File

@ -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();
}
}

View File

@ -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.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.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.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
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;
@ -35,6 +31,11 @@ class UsersControllerTests {
@Resource @Resource
private MockMvc mockMvc; private MockMvc mockMvc;
@BeforeEach
public void setup() {
}
@Test @Test
public void contextLoads() { public void contextLoads() {
// Test that the controller loads // Test that the controller loads
@ -70,7 +71,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
UsersController.wrapperUserAndPassword usersAndPassword = getWrapperUserAndPassword(); UsersController.wrapperUserAndPassword usersAndPassword = setWrapperUserAndPassword();
// Convert to JSON // Convert to JSON
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@ -86,7 +87,7 @@ class UsersControllerTests {
} }
@NotNull @NotNull
private UsersController.wrapperUserAndPassword getWrapperUserAndPassword() { private UsersController.wrapperUserAndPassword setWrapperUserAndPassword() {
Users user = new Users(); Users user = new Users();
user.setName("test"); user.setName("test");
user.setSex(""); user.setSex("");
@ -155,126 +156,3 @@ class UsersControllerTests {
.andReturn(); .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();
}
}