springboot + mybatis +junit UEL测试用例编写

springboot + mybatis +junit 测试用例编写

介绍

junit测试前段URL模拟测试
基于简单的查库主要体现junit的使用: github代码地址:https://github.com/lushunde321/springboot-junit.git

代码

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
 
	<groupId>com.lushunde.springboot.junit</groupId>
	<artifactId>spring-junit</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<name>spring-junit</name>
	<url>http://maven.apache.org</url>
 
	<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.7.RELEASE</version>
	</parent>
 
	<properties>
 
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
 
	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
 
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
 
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
 
 
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
 
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
 
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.28</version>
		</dependency>
 
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
 
	<!--maven的打包插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration><!-- 设置程序执行的主类 -->
					<mainClass>com.lushunde.springboot.junit.StartBootstarp</mainClass>
				</configuration>
 
			</plugin>
 
		</plugins>
 
		<!-- 配置java版本 不配置的话默认父类配置的是1.6 -->
		<pluginManagement>
			<plugins>
				<plugin>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.8</source>
						<target>1.8</target>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

junit单元测试代码

package com.lushunde.springboot.junit.controllerTest;
 
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
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.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
 
import com.alibaba.fastjson.JSON;
import com.lushunde.springboot.junit.StartBootstarp;
import com.lushunde.springboot.junit.model.User;
 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = StartBootstarp.class)
@WebAppConfiguration
public class UserControllerTest {
 
	@Autowired
	private WebApplicationContext context;
 
	private MockMvc mockMvc;
 
	@Before
	public void setupMockMvc() throws Exception {
		mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
	}
 
	/***
	 * 测试根据用户id获取用户信息接口
	 * 
	 * @throws Exception
	 */
	@Test
	public void getUserTest() throws Exception {
		String responce = mockMvc
				.perform(get("/user/getUser").contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的格式
						.param("token", "zhangsan") // 相当于直接写在url上的参数
						.param("id", "2"))
				.andExpect(status().isOk()) // 比较结果
				.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.id", is(2)))
				.andExpect(jsonPath("$.username", is("lisi"))).andExpect(jsonPath("$.age", is(21)))
				// .andDo(System.out.println()) //打印出请求和相应的内容
				.andReturn().getResponse().getContentAsString();
 
		System.out.println(responce);
	}
 
	/***
	 * 测试添加用户接口
	 * 
	 * @throws Exception
	 */
	@Test
	public void saveUserTest() throws Exception {
		// 构造添加的用户信息
		User user = new User();
		user.setAddress("上海市");
		user.setAge(69);
		user.setUsername("xiao7");
		user.setPassword("xiao7");
		System.out.println(JSON.toJSONString(user));
		// 调用接口,传入添加的用户参数
		String response = mockMvc
				.perform(post("/user/saveUser").contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(user))
						.header("SESSIONNO", "EA60F3C2C7384DBA8A7B8B114474DC12"))
				.andReturn().getResponse().getContentAsString();
		System.out.println(response);
 
	}
 
	/***
	 * 测试更新用户信息接口
	 * 
	 * @throws Exception
	 */
	@Test
	public void updateUserTest() throws Exception {
		User user = new User();
		user.setId(6l);
		user.setAddress("上海市");
		user.setAge(69);
		user.setUsername("xiao9");
		user.setPassword("xiao9");
		// 调用接口,传入添加的用户参数
		String response = mockMvc
				.perform(post("/user/updateUser").contentType(MediaType.APPLICATION_JSON)
						.content(JSON.toJSONString(user)).header("SESSIONNO", "EA60F3C2C7384DBA8A7B8B114474DC12"))
				.andReturn().getResponse().getContentAsString();
		System.out.println(response);
		System.out.println(response);
	}
 
	/***
	 * 测试获取用户列表接口
	 * 
	 * @throws Exception
	 */
	@Test
	public void deleteUser() throws Exception {
		RequestBuilder request = MockMvcRequestBuilders.get("/user/deleteUser?id=2");
 
		String perform = mockMvc.perform(request).andReturn().toString();
		System.out.println(perform);
	}
 
}

controller代码

package com.lushunde.springboot.junit.controller;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.lushunde.springboot.junit.model.User;
import com.lushunde.springboot.junit.service.UserService;
 
@RestController
@RequestMapping("user")
public class UserController {
	
	@Autowired 
	private UserService userService;
	
	@RequestMapping(value="getUser",method=RequestMethod.GET)
	public User getUserById(HttpServletRequest request, Integer id){
		User user = this.userService.getUserById(id);
		return user;
	}
	
	@RequestMapping(value="findByPage",method=RequestMethod.POST)
	public PageInfo<User> findByPage(HttpServletRequest request,Integer pageNo,Integer pageSize){
		PageInfo<User> list  = ((Page<User>)this.userService.findByPage(pageNo,pageSize)).toPageInfo();
		return list;
	}
	
	@RequestMapping(value="saveUser" ,method=RequestMethod.POST)
	public void saveUser(HttpServletRequest request,@RequestBody User user){
		System.out.println(request.getHeader("SESSIONNO"));
		this.userService.saveUser(user);
	}
	
	@RequestMapping(value="updateUser" ,method=RequestMethod.POST)
	public void updateUser(HttpServletRequest request,@RequestBody User user){
		this.userService.updateUser(user);
	}
	
	@RequestMapping(value="deleteUser",method=RequestMethod.GET )
	public Integer deleteUser(Long id){
		System.out.println(id);
		return this.userService.deleteUser(id);
	}
	
	
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值