Javaspring+mybit+maven中实现Junit测试类
在一个Javaspring+mybit+maven框架中,增加Junit测试类。
在测试类中遇到的一些问题,利用spring 框架时,里面已经有保密security+JWT设定的场合,在你的security文件中,添加你的URI路径,且确保完全一致,缺省的话是不可以的。
例如:
URI:aaaaa/bbb security文件中定义:aaaaa/** 的场合,测试类访问的时候会出现错误status:302
这种场合你就要尝试利用完全一致的写法来试验一下。
系统:window7
1.首先在配置文件pom.xml文件中添加测试类的包文件,之后执行maven取得相对应的包文件到本地
<dependency><!-- JUnit单元测试框架 -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency><!-- spring对测试框架的简单封装功能 -->
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency> <!--- mock追加->
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
测试类
场合1:当你的Controller里面没有用到session
package com.cnc.mht; import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.Assert;
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.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
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;
import java.util.Map;
import com.cnc.mht.web.http.RequestUtil;
import com.cnc.mht.web.weixin.dto.ImsWcyMakeAnAppointment;
import net.sf.json.JSONObject; @SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringJunitTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Autowired
JdbcTemplate jdbctemplate = null; // @Inject
@MockBean
// @Autowired
RequestUtil requestUtil; @Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
} @Test
public void testComment() throws Exception {
// parmがない
String responseString = mockMvc
.perform(MockMvcRequestBuilders.post("/openSesame").contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); System.out.println("哈哈哈" + responseString);
} @Test
public void testComment1() throws Exception {
// parmがある
String responseString = mockMvc
.perform(MockMvcRequestBuilders.post("/openSesame/getTableInfos")
.contentType(MediaType.APPLICATION_JSON_VALUE).param("weid", "11").param("bussinessid", "1")
.param("storeid", "53").param("date", "20190901"))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); System.out.println("哈哈哈" + responseString);
} @Test
public void testComment2() throws Exception {
// DB検索
List<Map<String, Object>> aa = jdbctemplate.queryForList("select * from ims_wcy_mst_code");
ImsWcyMakeAnAppointment imsWcyMakeAnAppointment = new ImsWcyMakeAnAppointment();
imsWcyMakeAnAppointment.setWeid(11);
imsWcyMakeAnAppointment.setBussinessid(1);
imsWcyMakeAnAppointment.setStoreid(53);
when(requestUtil.doPost(11, 1, 53, "/reserveCheck", null)).thenReturn("2");
// Controller侧的RequestBody是javabean的场合
// public String submit(@RequestBody ImsWcyMakeAnAppointment make,HttpServletRequest request){
String jsonObject = JSONObject.fromObject(imsWcyMakeAnAppointment).toString();
// Controller侧的RequestBody是Object[]的场合
// public String XXXXXX(HttpServletRequest request,@RequestBody Object[] dto) throws IOException{
Object[] dto = new String[] { "1", "1", "1" };
String a = JSONArray.fromObject(dto).toString();
// parmがJSON
String responseString = mockMvc.perform(MockMvcRequestBuilders.post("/openSesame/submit")
.contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonObject)).andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
// result検証
Assert.assertEquals(1, 1);
System.out.println("哈哈哈" + responseString);
}
}
运行即可
注※1 mock不好用的场合要确认被测试的类是否是@Service
场合2:当你的Controller里面有用到下面的代码
@RequestMapping(value = "/XXX", method = RequestMethod.POST)
// @RequestMapping("/mobilescreen/getfoodInfo")
public String XXX(String itemCode, String syokujiCode, HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
JSONArray json = new JSONArray();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
HttpSession session = request.getSession();
Login userInfo = (Login) session.getAttribute("LOGIN_USER");
}
测试类要修正
package com.cnc.mht.web.rest; import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; 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.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import com.cnc.mht.util.login.LoginUserInfoReform; @SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class XXX_test {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Autowired
JdbcTemplate jdbctemplate = null;
private MockHttpServletRequest request;
@Autowired
MockHttpSession session; @Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
} @Test
public void testComment1() throws Exception { Login aa = new Login();
aa.setBussinessid(1);
session.setAttribute("LOGIN_USER", aa);
// parmがある
ResultActions responseString = mockMvc
.perform(MockMvcRequestBuilders.post("/mobilescreen/getfoodInfo")
.contentType(MediaType.APPLICATION_JSON_VALUE).session(session).param("itemCode", "11")
.param("syokujiCode", "1"));
System.out.print("aaa"); } }
运行即可
Javaspring+mybit+maven中实现Junit测试类的更多相关文章
- Javaspring+mybit+maven中实现定时任务
背景:在Javaspring中,定时的启动某一个任务时,使用@Scheduled来实现 Javaspring工程创建好之后,直接创建下面的class文件即可.具体的用法可参照 https://www. ...
- Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)
1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...
- 在Eclipse中生成接口的JUnit测试类
在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...
- eclipse使用maven打包时去掉测试类
eclipse使用maven打包时去掉测试类 在pom.xml文件中增加如下配置: <plugin> <groupId>org.apache.maven.plugins< ...
- 15.junit测试类使用及注解
1.junit简介 JUnit是一个Java语言的单元测试框架,可以大大缩短你的测试时间和准确度.多数Java的开发环境都已经集成了JUnit作为单元测试的工具. 2.实现junitDemo示例 2. ...
- maven编译的时候排除junit测试类
maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...
- Junit测试类中如何调用Http通信
在使用Junit做测试的时候,有时候需要调用Http通信,无论是request还是response或者是session会话,那么在测试类里该如何调用呢,其实很简单,spring给我们提供了三个类 or ...
- java中使用junit测试
最初写代码只要功能走通就不管了,然后如果出了什么问题再去修改,这是因为没做测试的工作.测试其实很简单. 1.准备 当前使用idea编写代码,用maven构建工程,使用maven的test功能来进行批量 ...
- 高并发秒杀系统--junit测试类与SpringIoc容器的整合
1.原理是在Junit启动时加载SpringIoC容器 2.SpringIoC容器要根据Spring的配置文件加载 [示例代码] package org.azcode.dao; import org. ...
随机推荐
- SpringBoot终章(整合小型进销系统)
在前面的章节中我们学习Spring的时候可以看到配置文件比较多,所以我们有了SpringBoot 1. 引入依赖 <dependencies> <dependency> < ...
- 自用 goodsdetail
JSON.parse(data.parameter) 存的字符串 <select id="getGoodsBaseInfoById" resultType="co ...
- .net Web 项目的文件/文件夹上传下载
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
- Python 09 安装torch、torchvision
这个也是弄了我很久,百度了好多文章,其实像下面那样挺简单的,没那么复杂 1.进入torch的官网的下载页面,选择一下参数信息 地址:https://pytorch.org/get-started/lo ...
- nodejs+nvm历史版本
官网:http://nodejs.org/dist/ 淘宝镜像:https://npm.taobao.org/mirrors/node/ nvm历史版本:https://github.com/core ...
- javascript根据两点和底角,计算等腰三角形的顶点坐标
参考图: 代码如下: var x1 = 0; var y1 = 100; var x2 = -100; var y2 = 0; var angle = 30; var PI = Math.PI; // ...
- Time of Trial
Time of Trial(思维) 题目大意:一个人想逃,一个人要阻止那个人逃跑 ,阻止者念每一个符咒的时间为b[i],逃跑者念每一个符咒的时间为a[i],逃跑者如果念完了第i个符咒,阻止者还没有念完 ...
- MintUI引入vue项目以及引入iconfont图标
官网地址:http://mint-ui.github.io/#!/zh-cn 中文文档:http://mint-ui.github.io/docs/#/zh-cn2 示例展示:http://eleme ...
- 设计模式——<面向对象设计原则以及23种设计模式分类>
一.面向对象八大设计原则: 1.依赖倒置原则(DIP) 高层模块(稳定)不应该依赖于低层模块(变化),二者都应该依赖于抽象(稳定) . 抽象(稳定)不应该依赖于实现细节(变化) ,实现细节应该依赖于抽 ...
- D3.js的v5版本入门教程(第十三章)—— 饼状图
D3.js的v5版本入门教程(第十三章) 这一章我们来绘制一个简单的饼状图,我们只绘制构成饼状图基本的元素——扇形.文字,从这一章开始,内容可能有点难理解,因为每一章都会引入比较多的难理解知识点,在这 ...