单元测试

博客介绍了springTest的使用,包括导入springTest模块、给出springTest注入bean示例,以及模拟页面请求等内容,这些都是信息技术后端开发中关于框架使用的关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.springTest使用

1.导入springTest模块

<!-- spring-test 还需要引入junit -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.2.3.RELEASE</version>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
</dependency>

2.springTest 注入bean示例

import com.miracle.beans.Employee;
import com.miracle.dao.EmployeeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @ContextConfiguration 注解的 locations指定spring配置文件的位置
 *
 * @RunWith(SpringJUnit4ClassRunner.class) 指定使用哪种单元测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Test2 {

    // 这里可以直接注入依赖
    @Autowired
    EmployeeMapper employeeMapper;

    @Test
    public void t1(){
        Employee employee = employeeMapper.selectByPrimaryKeyWithDept(3);
        System.out.println(employee);
    }
}

3.springTest 模拟页面请求

package com.miracle;

import com.github.pagehelper.PageInfo;
import com.miracle.beans.Employee;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;

/**
 * 要想使用springTest 需要3.0以上版本的Servlet
 *
 * @ContextConfiguration 注解的 locations指定spring配置文件的位置,由于要测试请求,还要加上springMVC配置文件
 *
 * 加载配置文件路径还可以写 : file:src/main/webapp/WEB-INF/dispatcher-servlet.xml
 *
 * @RunWith(SpringJUnit4ClassRunner.class) 指定使用哪种单元测试
 */
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:dispatcher-servlet.xml"})
public class Test2 {

    // 虚拟的mvc请求,可以模拟浏览器请求
    private MockMvc mockMvc;

    /**
     * WebApplicationContext 为 springMVC的IOC容器
     *
     * 这里使用 @Autowired 注解自动装配,但是@Autowired只能装配IOC中的bean,
     * 无法装配IOC容器本身,需要在类上加额外配置 @WebAppConfiguration
     */
    @Autowired
    private WebApplicationContext webApplicationContext;

    // 初始化 MockMvc
    @Before
    public void initMockMvc(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void test1() throws Exception {
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pageNum", "1")).andReturn();
        // 请求成功以后,请求域中会有数据,去请求域拿数据验证
        MockHttpServletRequest request = mvcResult.getRequest();
        PageInfo pageInfo = (PageInfo)request.getAttribute("pageInfo");
        System.out.println("当前页码:" + pageInfo.getPageNum());
        System.out.println("总页码:"+ pageInfo.getPages());
        System.out.println("总记录数:"+ pageInfo.getTotal());
        int[] nums = pageInfo.getNavigatepageNums();
        for (int num : nums) {
            System.out.println("在页面需要连续显示的页码:"+ num);
        }
        // 获取员工信息
        List<Employee> list = pageInfo.getList();
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值