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. ...
随机推荐
- Spark API--Spark 分区
一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...
- 《团队作业第三、四周》五阿哥小组Scrum 冲刺阶段---Day2
<团队作业第三.四周>五阿哥小组Scrum 冲刺阶段---Day2 一.项目燃尽图 二.项目进展 20182310周烔今日进展: 主要任务一览:完成总博客的提交,制定接下来的计划,编写博客 ...
- C++中的类所占内存空间总结(转)
类所占内存的大小是由成员变量(静态变量除外)决定的,成员函数(这是笼统的说,后面会细说)是不计算在内的. 摘抄部分: 成员函数还是以一般的函数一样的存在.a.fun()是通过fun(a.this)来调 ...
- Spring框架:Controller和RestController区别
了解如何利用SpringMVC的注释创建RESTful Web服务. Spring的基于注释的MVC框架简化了创建RESTful Web服务的过程.传统的Spring MVC控制器和RESTful W ...
- 【NOIP2015】斗地主 D1 T3 及 增强版 (送命题)
恶心送命模拟题 暴搜顺子,DP预处理剩下的. 由于官方数据太水,很多情况没有讨论的都能过普通版本,想要测试自己代码正确性的同学们可以交交这道题,有很多dalao给出了hack数据 : Luogu P2 ...
- 08-Flutter移动电商实战-dio基础_伪造请求头获取数据
在很多时候,后端为了安全都会有一些请求头的限制,只有请求头对了,才能正确返回数据.这虽然限制了一些人恶意请求数据,但是对于我们聪明的程序员来说,就是形同虚设.这篇文章就以极客时间 为例,讲一下通过伪造 ...
- H5页面中判断是安卓手机还是ios手机的方法;APP页面中嵌套的H5跳转到APP其他页面的方法。
(一).在H5页面中,可以直接利用如下的方法来进行判断是安卓还是ios. var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linu ...
- windows串口编程Win32,PComm串口开发
https://blog.csdn.net/u011430225/article/details/51496456 https://blog.csdn.net/eit520/article/detai ...
- 50、Spark Streaming实时wordcount程序开发
一.java版本 package cn.spark.study.streaming; import java.util.Arrays; import org.apache.spark.SparkCon ...
- 咕泡学院java架构vip课程
1.wps文档地址 https://docs.qq.com/doc/DRVNLUndvTmFSdEhO 2.百度网盘地址 https://pan.baidu.com/s/1uxaTzJZHKrsw_H ...