SSM框架的(CRUD)查詢_使用Spring單元測試測試分頁請求7.0

1、test文件夾新建MvcTest.java類進(jìn)行測試

/**
 * 使用Spring測試模塊提供的測試請求功能,測試curd請求的正確性
 * Spring4測試的時候,需要servlet3.0的支持
 * @author zhangcheng
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml",
        "file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml" })
public class MvcTest {
    // 傳入Springmvc的ioc
    @Autowired
    WebApplicationContext context;
    // 虛擬mvc請求,獲取到處理結(jié)果。
    MockMvc mockMvc;

    @Before
    public void initMokcMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void testPage() throws Exception {
        // 模擬請求拿到返回值
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).andReturn();
        // 請求成功以后,請求域中會有pageInfo;我們可以取出pageInfo進(jìn)行驗證
        MockHttpServletRequest request = result.getRequest();
        PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
        System.out.println("當(dāng)前頁碼:" + pi.getPageNum());
        System.out.println("總頁碼:" + pi.getPages());
        System.out.println("總記錄數(shù):" + pi.getTotal());
        System.out.println("在頁面需要連續(xù)顯示的頁碼");
        int[] nums = pi.getNavigatepageNums();
        for (int i : nums) {
            System.out.print(" " + i);
        }
        // 獲取員工數(shù)據(jù)
        List<Employee> list = pi.getList();
        for (Employee employee : list) {
            System.out.println("ID:" + employee.getEmpId() + "==>Name:" + employee.getEmpName());
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容