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. ...
随机推荐
- linux 读取文件
linux读取文件是经常要用到的操作,以下示例(说明看注释): #读取文件snlist.txt中的每一行内容赋给sn变量 while read sn do echo ">>> ...
- presto docker简单试用
starburstdata 团队提供了一个docker 版本的presto,其中已经内置了几个connectors tpch tpcds memory backhole jmx system pull ...
- apisix 基于openresty 的api 网关
apisix 是由openresty 团队开发并开源的微服务api gateway,还不错,官方文档也比较全,同时这个也是一个不错的学习openresty 的项目 以下为来自官方的架构图 插件加载 插 ...
- 洛谷P2744 量取牛奶
题目 DP或者迭代加深搜索,比较考验递归的搜索. 题目第一问可以用迭代加深搜索限制层数. 第二问需要满足字典序最小,所以我们可以在搜索的时候把比当前答案字典序大的情况剪枝掉. 然后考虑怎么搜索,对于每 ...
- shell脚本编程基础之while、for、until循环
while及until循环结构 while CONDITION:do statement done 进入循环:条件满足 退出循环:条件不满足 当需要命令的执行状态返回值时,可以直接把整个命令当做循环的 ...
- 计蒜客——Reversion Count
Reversion Count 解析:题目数字的长度最大为 99,因此使用字符串处理,那么必然这些数存在某些规律.对于数 a (XYZW) 和数 b (WZYX),原式 = (1000X + 100Y ...
- Detection of Glacier Calving Margins with Convolutional Neural Networks: A Case Study
利用Unet结构对landsat数据进行冰川裂缝提取,结构如下:训练集很小只有123张152*240图片
- mysql round()函数以及convert()函数,保留n位小数
mysql> ); +----------------+ | round() | +----------------+ | 2.23 | +----------------+ row in se ...
- Shiro安全框架案例
基于Shiro的用户认证(不包含授权) Spring整合Shiro shiro原理 1.1 搭建环境 1.1.1 web模块 pom.xml <dependency> < ...
- element ui 中的时间选择器怎么设置默认值/el-date-picker区间选择器怎么这是默认值
template代码 <el-date-picker value-format="yyyy-MM-dd" v-model="search.date" ty ...