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