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);
	}
	
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章