main
your-name 1 week ago
parent 33ab27df69
commit f9ab1e60cd

@ -1,34 +1,35 @@
package com.ssm.controller;
import com.ssm.Utils.PageBean;
import com.ssm.entity.Demo;
import com.ssm.service.DemoService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.ModelAndView;
import java.util.Objects;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* DemoController
* Demo
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class DemoControllerTest {
private MockMvc mockMvc;
private AutoCloseable mocks;
@Mock
private DemoService demoService;
@ -45,143 +46,250 @@ public class DemoControllerTest {
@Before
public void setUp() {
// 初始化Mock注解使用旧版API适配mockito 2.23.4
MockitoAnnotations.initMocks(this);
mocks = MockitoAnnotations.openMocks(this);
// 构建MockMvc
mockMvc = MockMvcBuilders.standaloneSetup(demoController).build();
}
/**
*
* Mock
*/
@After
public void tearDown() throws Exception {
// mockito 2.23.4 使用 initMocks无需手动清理
if (mocks != null) {
mocks.close();
}
}
/**
* updateOk -
* add -
*/
@Test
public void testUpdateOk_Success() throws Exception {
public void testAdd_ReturnAddPage() throws Exception {
// 执行测试
ModelAndView mv = demoController.add();
// 验证返回的视图名称
assertEquals("admin/demo/add", mv.getViewName());
}
/**
* findAllByPage -
*/
@Test
public void testFindAllByPage_Success() throws Exception {
// 准备测试数据
Demo demo = new Demo(1, "测试Demo", "测试备注");
int page = 1;
PageBean<Demo> pageBean = new PageBean<>();
pageBean.setPage(page);
pageBean.setLimitPage(6);
pageBean.setTotlePage(2);
pageBean.setList(Arrays.asList(new Demo(1, "测试1", "备注1")));
// 执行测试模拟POST请求到/updateOk
mockMvc.perform(post("/demo/updateOk")
.param("did", "1")
.param("dname", "测试Demo")
.param("comment", "测试备注")
when(demoService.getDemoAll(page)).thenReturn(pageBean);
// 执行测试
mockMvc.perform(get("/demo/findAllByPage")
.param("page", String.valueOf(page))
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isOk())
.andExpect(view().name("admin/demo/list"))
.andExpect(model().attributeExists("allProPageBean"));
// 验证服务层方法被调用
verify(demoService, times(1)).getDemoAll(page);
}
/**
* findById - ID
*/
@Test
public void testFindById_Success() throws Exception {
// 准备测试数据
Integer did = 1;
Demo demo = new Demo(did, "测试Demo", "测试备注");
when(demoService.getDemoById(did)).thenReturn(demo);
// 执行测试
mockMvc.perform(get("/demo/findById")
.param("did", String.valueOf(did))
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isOk())
.andExpect(view().name("admin/demo/look"))
.andExpect(model().attribute("findByDid", demo));
// 验证服务层方法被调用
verify(demoService, times(1)).getDemoById(did);
}
/**
* findById - ID
*/
@Test
public void testFindById_NotFound() throws Exception {
// 准备测试数据
Integer did = 999;
when(demoService.getDemoById(did)).thenReturn(null);
// 执行测试
mockMvc.perform(get("/demo/findById")
.param("did", String.valueOf(did))
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isOk())
.andExpect(view().name("admin/demo/look"))
.andExpect(model().attribute("findByDid", null));
// 验证服务层方法被调用
verify(demoService, times(1)).getDemoById(did);
}
/**
* addok -
*/
@Test
public void testAddOk_Success() throws Exception {
// 执行测试
mockMvc.perform(get("/demo/addOk")
.param("dname", "新Demo")
.param("comment", "新备注")
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/demo/findAllByPage.action?page=1"));
.andExpect(redirectedUrl(REDIRECT_URL));
// 验证demoService.updateDemo方法被调用了一次
verify(demoService, times(1)).updateDemo(any(Demo.class));
// 验证服务层方法被调用
verify(demoService, times(1)).addDemo(any(Demo.class));
}
/**
* updateOk - Demo
* updateById - ID
*/
@Test
public void testUpdateOk_DemoObjectPassed() throws Exception {
public void testUpdateById_Success() throws Exception {
// 准备测试数据
Integer expectedId = 2;
String expectedName = "更新后的名称";
String expectedComment = "更新后的备注";
Integer did = 1;
Demo demo = new Demo(did, "待更新Demo", "待更新备注");
when(demoService.getDemoById(did)).thenReturn(demo);
// 执行测试
mockMvc.perform(post("/demo/updateOk")
.param("did", expectedId.toString())
.param("dname", expectedName)
.param("comment", expectedComment)
.contentType(MediaType.APPLICATION_FORM_URLENCODED));
mockMvc.perform(get("/demo/updateById")
.param("did", String.valueOf(did))
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isOk())
.andExpect(view().name("admin/demo/edit"))
.andExpect(model().attribute("findByDid", demo));
// 验证updateDemo被调用并且传入的Demo对象属性正确
verify(demoService).updateDemo(argThat(demo ->
demo != null &&
Objects.equals(expectedId, demo.getDid()) &&
Objects.equals(expectedName, demo.getDname()) &&
Objects.equals(expectedComment, demo.getComment())
));
// 验证服务层方法被调用
verify(demoService, times(1)).getDemoById(did);
}
/**
* updateOk - URL
* updateOk -
*/
@Test
public void testUpdateOk_RedirectUrl() throws Exception {
public void testUpdateOk_Success() throws Exception {
// 执行测试
mockMvc.perform(post("/demo/updateOk")
mockMvc.perform(get("/demo/updateOk")
.param("did", "1")
.param("dname", "测试")
.param("comment", "备注")
.param("dname", "更新后的Demo")
.param("comment", "更新后的备注")
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isFound()) // 302重定向
.andExpect(redirectedUrl("/demo/findAllByPage.action?page=1"));
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl(REDIRECT_URL));
// 验证服务层方法被调用
verify(demoService, times(1)).updateDemo(any(Demo.class));
}
/**
* updateOk -
* deleteById -
*/
@Test
public void testUpdateOk_ServiceCalledOnce() throws Exception {
public void testDeleteById_Success() throws Exception {
// 执行测试
mockMvc.perform(post("/demo/updateOk")
mockMvc.perform(get("/demo/deleteById")
.param("did", "1")
.param("dname", "测试")
.param("comment", "备注")
.contentType(MediaType.APPLICATION_FORM_URLENCODED));
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl(REDIRECT_URL));
// 严格验证updateDemo方法只被调用一次
verify(demoService, times(1)).updateDemo(any(Demo.class));
// 验证没有其他方法被调用
verifyNoMoreInteractions(demoService);
// 验证服务层方法被调用
verify(demoService, times(1)).deleteDemoById(1);
}
/**
* updateOk -
* searchDemoByCondition -
*/
@Test
public void testUpdateOk_EmptyComment() throws Exception {
// 执行测试:备注为空
mockMvc.perform(post("/demo/updateOk")
.param("did", "1")
.param("dname", "测试Demo")
.param("comment", "")
public void testSearchDemoByCondition_Found() throws Exception {
// 准备测试数据
String condition = "测试";
List<Demo> demoList = Arrays.asList(
new Demo(1, "测试1", "备注1"),
new Demo(2, "测试2", "备注2")
);
when(demoService.searchDemoByCondition(condition)).thenReturn(demoList);
// 执行测试
mockMvc.perform(get("/demo/searchDemoByCondition")
.param("condition", condition)
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().is3xxRedirection());
.andExpect(status().isOk())
.andExpect(view().name("admin/demo/search"))
.andExpect(model().attributeExists("srList"))
.andExpect(model().attribute("srList", demoList));
// 验证仍然调用了updateDemo
verify(demoService, times(1)).updateDemo(argThat(demo ->
demo != null && Objects.equals("", demo.getComment())
));
// 验证服务层方法被调用
verify(demoService, times(1)).searchDemoByCondition(condition);
}
/**
* updateOk -
* searchDemoByCondition -
*/
@Test
public void testUpdateOk_SpecialCharacters() throws Exception {
// 执行测试:包含特殊字符
String specialName = "测试<Demo>&名称";
String specialComment = "备注'内容\"测试";
mockMvc.perform(post("/demo/updateOk")
.param("did", "1")
.param("dname", specialName)
.param("comment", specialComment)
public void testSearchDemoByCondition_NotFound() throws Exception {
// 准备测试数据
String condition = "不存在的条件";
List<Demo> emptyList = Arrays.asList();
when(demoService.searchDemoByCondition(condition)).thenReturn(emptyList);
// 执行测试
mockMvc.perform(get("/demo/searchDemoByCondition")
.param("condition", condition)
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().is3xxRedirection());
.andExpect(status().isOk())
.andExpect(view().name("admin/demo/search"))
.andExpect(model().attribute("srList", emptyList));
// 验证特殊字符能正确传递
verify(demoService).updateDemo(argThat(demo ->
demo != null &&
Objects.equals(specialName, demo.getDname()) &&
Objects.equals(specialComment, demo.getComment())
));
// 验证服务层方法被调用
verify(demoService, times(1)).searchDemoByCondition(condition);
}
/**
* findDemoByDid -
*/
@Test
public void testFindDemoByDid_Success() throws Exception {
// 准备测试数据
int did = 1;
Demo demo = new Demo(did, "前台展示Demo", "前台备注");
when(demoService.getDemoById(did)).thenReturn(demo);
// 执行测试
mockMvc.perform(get("/demo/findDemoByDid")
.param("did", String.valueOf(did))
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isOk())
.andExpect(view().name("demo"))
.andExpect(model().attribute("demo", demo));
// 验证服务层方法被调用
verify(demoService, times(1)).getDemoById(did);
}
/**
@ -194,4 +302,53 @@ public class DemoControllerTest {
// 验证demoService已被注入虽然是mock对象
assertNotNull("DemoService应被注入", demoController.demoService);
}
/**
* addok - Demo
*/
@Test
public void testAddOk_DemoObjectPassed() throws Exception {
// 执行测试
String expectedName = "新Demo名称";
String expectedComment = "新Demo备注";
mockMvc.perform(get("/demo/addOk")
.param("dname", expectedName)
.param("comment", expectedComment)
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().is3xxRedirection());
// 验证addDemo被调用并且传入的Demo对象属性正确
verify(demoService).addDemo(argThat(demo ->
demo != null &&
expectedName.equals(demo.getDname()) &&
expectedComment.equals(demo.getComment())
));
}
/**
* updateOk - Demo
*/
@Test
public void testUpdateOk_DemoObjectPassed() throws Exception {
// 执行测试
String expectedId = "1";
String expectedName = "更新后的名称";
String expectedComment = "更新后的备注";
mockMvc.perform(get("/demo/updateOk")
.param("did", expectedId)
.param("dname", expectedName)
.param("comment", expectedComment)
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().is3xxRedirection());
// 验证updateDemo被调用并且传入的Demo对象属性正确
verify(demoService).updateDemo(argThat(demo ->
demo != null &&
expectedId.equals(String.valueOf(demo.getDid())) &&
expectedName.equals(demo.getDname()) &&
expectedComment.equals(demo.getComment())
));
}
}

@ -0,0 +1,463 @@
package com.ssm.service;
import com.ssm.Utils.PageBean;
import com.ssm.entity.Demo;
import com.ssm.mapper.DemoMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* DemoService
* Demo
*/
public class DemoServiceTest {
@Mock
private DemoMapper demoMapper;
@InjectMocks
private DemoService demoService;
// 测试常量
private static final Integer TEST_ID = 1;
private static final String TEST_NAME = "测试Demo";
private static final String TEST_COMMENT = "测试备注";
private static final int PAGE_SIZE = 6;
/**
* Mock
*/
@Before
public void setUp() {
// 初始化Mock注解使用旧版API适配mockito 2.23.4
MockitoAnnotations.initMocks(this);
}
/**
*
*/
@After
public void tearDown() throws Exception {
// mockito 2.23.4 使用 initMocks无需手动清理
}
/**
* getDemoAll -
*/
@Test
public void testGetDemoAll_FirstPage() {
// 准备测试数据
int page = 1;
int totalCount = 15; // 总共15条记录
List<Demo> mockList = Arrays.asList(
new Demo(1, "Demo1", "备注1"),
new Demo(2, "Demo2", "备注2")
);
// Mock行为
when(demoMapper.countTotlePage()).thenReturn(totalCount);
when(demoMapper.getDemoAll(0, PAGE_SIZE)).thenReturn(mockList);
// 执行测试
PageBean<Demo> result = demoService.getDemoAll(page);
// 验证结果
assertNotNull("PageBean不应为null", result);
assertEquals("当前页码应为1", 1, result.getPage());
assertEquals("每页数量应为6", PAGE_SIZE, result.getLimitPage());
assertEquals("总页数应为3", 3, result.getTotlePage()); // 15/6=2.5向上取整为3
assertNotNull("列表不应为null", result.getList());
assertEquals("列表大小应为2", 2, result.getList().size());
// 验证调用
verify(demoMapper, times(1)).countTotlePage();
verify(demoMapper, times(1)).getDemoAll(0, PAGE_SIZE);
}
/**
* getDemoAll -
*/
@Test
public void testGetDemoAll_SecondPage() {
// 准备测试数据
int page = 2;
int totalCount = 15;
List<Demo> mockList = Arrays.asList(
new Demo(7, "Demo7", "备注7"),
new Demo(8, "Demo8", "备注8")
);
// Mock行为
when(demoMapper.countTotlePage()).thenReturn(totalCount);
when(demoMapper.getDemoAll(6, PAGE_SIZE)).thenReturn(mockList);
// 执行测试
PageBean<Demo> result = demoService.getDemoAll(page);
// 验证结果
assertNotNull("PageBean不应为null", result);
assertEquals("当前页码应为2", 2, result.getPage());
assertEquals("总页数应为3", 3, result.getTotlePage());
assertEquals("列表大小应为2", 2, result.getList().size());
// 验证调用beginPage应该是(2-1)*6=6
verify(demoMapper).getDemoAll(6, PAGE_SIZE);
}
/**
* getDemoAll -
*/
@Test
public void testGetDemoAll_LastPage() {
// 准备测试数据
int page = 3;
int totalCount = 15;
List<Demo> mockList = Collections.singletonList(
new Demo(13, "Demo13", "备注13")
);
// Mock行为
when(demoMapper.countTotlePage()).thenReturn(totalCount);
when(demoMapper.getDemoAll(12, PAGE_SIZE)).thenReturn(mockList);
// 执行测试
PageBean<Demo> result = demoService.getDemoAll(page);
// 验证结果
assertNotNull("PageBean不应为null", result);
assertEquals("当前页码应为3", 3, result.getPage());
assertEquals("总页数应为3", 3, result.getTotlePage());
assertEquals("列表大小应为1", 1, result.getList().size());
// 验证调用beginPage应该是(3-1)*6=12
verify(demoMapper).getDemoAll(12, PAGE_SIZE);
}
/**
* getDemoAll -
*/
@Test
public void testGetDemoAll_EmptyResult() {
// 准备测试数据
int page = 1;
int totalCount = 0;
List<Demo> mockList = Collections.emptyList();
// Mock行为
when(demoMapper.countTotlePage()).thenReturn(totalCount);
when(demoMapper.getDemoAll(0, PAGE_SIZE)).thenReturn(mockList);
// 执行测试
PageBean<Demo> result = demoService.getDemoAll(page);
// 验证结果
assertNotNull("PageBean不应为null", result);
assertEquals("当前页码应为1", 1, result.getPage());
assertEquals("总页数应为0", 0, result.getTotlePage());
assertNotNull("列表不应为null", result.getList());
assertTrue("列表应为空", result.getList().isEmpty());
}
/**
* getDemoAll -
*/
@Test
public void testGetDemoAll_ExactDivision() {
// 准备测试数据18条记录每页6条正好3页
int page = 1;
int totalCount = 18;
List<Demo> mockList = Arrays.asList(
new Demo(1, "Demo1", "备注1"),
new Demo(2, "Demo2", "备注2")
);
// Mock行为
when(demoMapper.countTotlePage()).thenReturn(totalCount);
when(demoMapper.getDemoAll(0, PAGE_SIZE)).thenReturn(mockList);
// 执行测试
PageBean<Demo> result = demoService.getDemoAll(page);
// 验证结果18/6=3应该正好3页
assertEquals("总页数应为3", 3, result.getTotlePage());
}
/**
* getDemoById -
*/
@Test
public void testGetDemoById_Success() {
// 准备测试数据
Demo expectedDemo = new Demo(TEST_ID, TEST_NAME, TEST_COMMENT);
// Mock行为
when(demoMapper.findDemoByDid(TEST_ID)).thenReturn(expectedDemo);
// 执行测试
Demo result = demoService.getDemoById(TEST_ID);
// 验证结果
assertNotNull("Demo不应为null", result);
assertEquals("ID应匹配", TEST_ID, result.getDid());
assertEquals("名称应匹配", TEST_NAME, result.getDname());
assertEquals("备注应匹配", TEST_COMMENT, result.getComment());
// 验证调用
verify(demoMapper, times(1)).findDemoByDid(TEST_ID);
}
/**
* getDemoById - ID
*/
@Test
public void testGetDemoById_NotFound() {
// Mock行为返回null
when(demoMapper.findDemoByDid(999)).thenReturn(null);
// 执行测试
Demo result = demoService.getDemoById(999);
// 验证结果
assertNull("不存在的ID应返回null", result);
}
/**
* addDemo -
*/
@Test
public void testAddDemo_Success() {
// 准备测试数据
Demo demo = new Demo(TEST_NAME, TEST_COMMENT);
// 执行测试
demoService.addDemo(demo);
// 验证调用
verify(demoMapper, times(1)).addDemo(demo);
}
/**
* addDemo -
*/
@Test(expected = NullPointerException.class)
public void testAddDemo_NullDemo() {
// 执行测试应该抛出NullPointerException
demoService.addDemo(null);
}
/**
* updateDemo -
*/
@Test
public void testUpdateDemo_Success() {
// 准备测试数据
Demo demo = new Demo(TEST_ID, TEST_NAME, TEST_COMMENT);
// 执行测试
demoService.updateDemo(demo);
// 验证调用
verify(demoMapper, times(1)).updateDemo(demo);
}
/**
* updateDemo -
*/
@Test
public void testUpdateDemo_NonExistent() {
// 准备测试数据
Demo demo = new Demo(999, "不存在的Demo", "备注");
// 执行测试
demoService.updateDemo(demo);
// 验证调用仍然发生(由数据库层处理不存在的情况)
verify(demoMapper, times(1)).updateDemo(demo);
}
/**
* deleteDemoById -
*/
@Test
public void testDeleteDemoById_Success() {
// 执行测试
demoService.deleteDemoById(TEST_ID);
// 验证调用
verify(demoMapper, times(1)).deleteDemoById(TEST_ID);
}
/**
* deleteDemoById - ID
*/
@Test
public void testDeleteDemoById_NonExistent() {
// 执行测试
demoService.deleteDemoById(999);
// 验证调用仍然发生(由数据库层处理不存在的情况)
verify(demoMapper, times(1)).deleteDemoById(999);
}
/**
* searchDemoByCondition -
*/
@Test
public void testSearchDemoByCondition_Success() {
// 准备测试数据
String condition = "测试";
List<Demo> expectedList = Arrays.asList(
new Demo(1, "测试Demo1", "测试备注"),
new Demo(2, "测试Demo2", "普通备注")
);
// Mock行为
when(demoMapper.searchDemoByCondition(condition)).thenReturn(expectedList);
// 执行测试
List<Demo> result = demoService.searchDemoByCondition(condition);
// 验证结果
assertNotNull("搜索结果不应为null", result);
assertEquals("结果大小应为2", 2, result.size());
// 验证调用
verify(demoMapper, times(1)).searchDemoByCondition(condition);
}
/**
* searchDemoByCondition -
*/
@Test
public void testSearchDemoByCondition_EmptyCondition() {
// 准备测试数据
String condition = "";
List<Demo> expectedList = Collections.emptyList();
// Mock行为
when(demoMapper.searchDemoByCondition(condition)).thenReturn(expectedList);
// 执行测试
List<Demo> result = demoService.searchDemoByCondition(condition);
// 验证结果
assertNotNull("搜索结果不应为null", result);
assertTrue("空条件应返回空列表", result.isEmpty());
}
/**
* searchDemoByCondition -
*/
@Test
public void testSearchDemoByCondition_NoMatch() {
// 准备测试数据
String condition = "不存在的关键词";
List<Demo> expectedList = Collections.emptyList();
// Mock行为
when(demoMapper.searchDemoByCondition(condition)).thenReturn(expectedList);
// 执行测试
List<Demo> result = demoService.searchDemoByCondition(condition);
// 验证结果
assertTrue("无匹配应返回空列表", result.isEmpty());
}
/**
* findDemoByDId -
*/
@Test
public void testFindDemoByDId_Success() {
// 准备测试数据
List<Demo> expectedList = Arrays.asList(
new Demo(1, "Demo1", "备注1"),
new Demo(2, "Demo2", "备注2")
);
// Mock行为
when(demoMapper.findDemoByDId(TEST_ID)).thenReturn(expectedList);
// 执行测试
List<Demo> result = demoService.findDemoByDId(TEST_ID);
// 验证结果
assertNotNull("结果不应为null", result);
assertEquals("结果大小应为2", 2, result.size());
// 验证调用
verify(demoMapper, times(1)).findDemoByDId(TEST_ID);
}
/**
* findDemoByDId -
*/
@Test
public void testFindDemoByDId_EmptyResult() {
// Mock行为
when(demoMapper.findDemoByDId(999)).thenReturn(Collections.emptyList());
// 执行测试
List<Demo> result = demoService.findDemoByDId(999);
// 验证结果
assertNotNull("结果不应为null", result);
assertTrue("结果应为空列表", result.isEmpty());
}
/**
* -
*/
@Test
public void testPagination_BoundaryValue() {
// 准备测试数据13条记录每页6条应该是3页13/6=2.166...向上取整为3
int page = 1;
int totalCount = 13;
List<Demo> mockList = Collections.emptyList();
// Mock行为
when(demoMapper.countTotlePage()).thenReturn(totalCount);
when(demoMapper.getDemoAll(0, PAGE_SIZE)).thenReturn(mockList);
// 执行测试
PageBean<Demo> result = demoService.getDemoAll(page);
// 验证结果应该有3页
assertEquals("总页数应为3", 3, result.getTotlePage());
}
/**
* -
*/
@Test
public void testPagination_SingleRecord() {
// 准备测试数据
int page = 1;
int totalCount = 1;
List<Demo> mockList = Collections.singletonList(
new Demo(1, "唯一记录", "备注")
);
// Mock行为
when(demoMapper.countTotlePage()).thenReturn(totalCount);
when(demoMapper.getDemoAll(0, PAGE_SIZE)).thenReturn(mockList);
// 执行测试
PageBean<Demo> result = demoService.getDemoAll(page);
// 验证结果
assertEquals("总页数应为1", 1, result.getTotlePage());
assertEquals("列表大小应为1", 1, result.getList().size());
}
}
Loading…
Cancel
Save