spring controller 方法测试

controller 测试 不使用其他api接口测试工具

一般而言,我们写好一个模块后,会对其进行单元测试,再集成到现有的系统中。

但是呢~针对Controller、Service、Dao三层来说,我们最常的是对Service和Dao进行单元测试。然而Controller的测试,很多人还是启动Tomcat,使用Postman进行api测试,这样不仅需要等待很长的编译部署时间,而且无法逐个Controller功能进行单独测试,因此特意总结Controller层的单元测试。顺便说下,Postman挺好用的,只是我们仍然需要启动tomcat,有点浪费时间。

那么,我们需要有一种不需启动tomcat就可以测试controller层的方法。

接下来我们细细讲来。

controller所采用的是SpringMVC框架 。SpringMVC已经继承了单元测试的类

步骤:

1.创建普通类文件

2.引入Spring单元测试注释

@RunWith(SpringJUnit4ClassRunner.class) // 此处调用Spring单元测试类
@WebAppConfiguration // 调用javaWEB的组件,比如自动注入ServletContext Bean等等
@ContextConfiguration(locations = { "classpath:context.xml", "classpath:mvccontext.xml" }) // 加载Spring配置文件
public class TestPayTypeController {
    @Autowired
    PayTypeController payTypeController;//测试的controller类

    @Autowired
    ServletContext context;

    MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(payTypeController).build();
    }
}

注意:

@WebAppConfiguration如果不加的话,是没法调用WEB的一些特性的。经常会被遗忘掉。。。

@ContextConfiguration中,需要把所有Spring的配置文件全部加载进来,因为有的项目中Spring 的xml配置是分拆的。 此处的xml是放在resources的根目录中。

3.引入spring注解后,Controller的单元测试需要模拟Server的运行,需要在class中进行WEB环境的初始化。

MockMvc是SpringMVC提供的Controller测试类

每次进行单元测试时,都是预先执行@Before中的setup方法,初始化PayTypeController单元测试环境。

4.前期准备工作都做好了。可以编写单元测试方法了。

先看get方法请求

ResultAction是用来模拟Browser发送FORM表单请求的。get()是请求的地址,accept()请求的内容 ;

@org.junit.Test // get请求
    public void getListTest() throws Exception {
        // 发送请求
        ResultActions resultActions = this.mockMvc
                .perform(MockMvcRequestBuilders.get("/paytype/list/all").accept(MediaType.APPLICATION_JSON));
        MvcResult mvcResult = resultActions.andReturn();
        String result = mvcResult.getResponse().getContentAsString();
        System.out.println("客户端获的数据:" + result);
    }

 在控制台打印如下,说明成功了!

 

接下来使用post 请求;

ResultAction是用来模拟Browser发送FORM表单请求的。post()是请求的地址,accept()请求的内容 param()请求的键值对,如果有多个参数可以后缀调用多个param();

MvcResult是获得服务器的Response内容。

@org.junit.Test // post请求
    public void addTest() throws Exception {
        // 发送请求
        ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/paytype/add")
                .accept(MediaType.APPLICATION_JSON).param("typename", "一年停").param("payfee","4444.0").param("payto", "post"));
        MvcResult mvcResult = resultActions.andReturn();
        String result = mvcResult.getResponse().getContentAsString();
        System.out.println("客户端获的数据:" + result);
    }

 成功插入数据库;

一般常用的就get,post请求;至此,我们不需启动tomcat就能测试controller层的method。

 附上controller类的method

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(PayTypeModel ptm) throws Exception {
        ResultMessage result = new ResultMessage();
        payTypeService.add(ptm);
        result.setResult("Y");
        result.setMessage("增加缴费类型成功");
        return result;

    }



     @RequestMapping(value="/get",method=RequestMethod.GET)
     public PayTypeModel get(@RequestParam int typeno) throws Exception
      {
           return payTypeService.get(typeno);
      }    

如有错误,欢迎留言指正!

原文地址:https://www.cnblogs.com/sunTin/p/7590867.html