SpringBoot Mock测试
来源:互联网 发布:ipad设计软件 编辑:程序博客网 时间:2024/05/17 07:30
今天用Mock测试Controller的时候,刚开始发现Controller里面的Service没有被实例化,也就是没有注入bean。
原先的测试代码,我没有通过AutoWired注解注入repaymentScheduleController,而是直接在setup里面new了一个Controller。
正常运行后的代码:
package com.creditease.microloan.mil.loanrepay;import com.creditease.microloan.mil.loanrepay.controller.RepaymentScheduleController;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringRunner;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.result.MockMvcResultHandlers;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.test.web.servlet.setup.MockMvcBuilders;@RunWith(SpringRunner.class)@SpringBootTest(classes = Application.class)public class MicroloanMilLoanRepayApplicationTests {/* 测试service@Autowiredprivate RepaymentCalculateService repaymentCalculateService;@Testpublic void test2(){List<RepayTermBO> list = repaymentCalculateService.getRepayTerm(50, Timestamp.valueOf("2017-05-22 00:00:00"),1,10);Assert.assertNotNull(list);}@Testpublic void test3(){List<RepayTermBO> list = repaymentCalculateService.getRepayTerm(50, Timestamp.valueOf("2017-05-22 00:00:00"),1,10);Assert.assertNotNull(list);}*/ //测试Controller接口@Autowiredprivate RepaymentScheduleController repaymentScheduleController;private MockMvc mockMvc;@Beforepublic void setup(){mockMvc = MockMvcBuilders.standaloneSetup(repaymentScheduleController).build();}//验证controller是否正常响应并打印返回结果@Testpublic void getHello() throws Exception {MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/loanrepay/repaymentSchedule/repayTerm").accept(MediaType.APPLICATION_JSON).param("dayNum","8").param("date","2017-7-18 00:00:00").param("pageNum","1").param("pageSize","10")).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();System.out.println("输出 " + mvcResult.getResponse().getContentAsString());}}
阅读全文
0 0
- SpringBoot Mock测试
- mock 测试
- mock测试
- MOCK测试
- mock测试
- mock 测试
- mock测试
- Mock测试
- mock测试
- mock测试
- mock测试
- mock测试
- Mock测试
- springboot+mockito (二)mock
- java mock 测试
- 什么是mock测试
- mock测试定义
- 使用Spring mock测试
- java基础知识点汇总
- NOIP2017提高组模拟赛 10 (总结)
- activeMQ主要的几类集群部署方式
- 【JVM】类文件结构
- 2017 随笔一
- SpringBoot Mock测试
- VS2010利用ATL创建COM dll,编译时报错
- DEV gridview 捕获行双击事件
- JZOJ 3765【BJOI2014】想法
- 前端开发-js正则总结
- iOS 裁剪圆形图像并显示(类似于微信头像)
- 关于struts的ognl表达式
- Java面试宝典2017版
- windows下搭建基于nginx的rtmp服务器