SpringBoot测试用例

泄露秘密 提交于 2019-12-17 04:51:43

@RunWith(SpringRunner.class)
如何运行这个测试类,这里用SpringRunner运行测试用例
@SpringBootTest
代表这是一个测试类
@Test
测试方法

WEB项目需一个MVC环境

搭建一个简单Spring Boot的被测试环境

新建一个User对象

package com.user.domain;

public class User {
	
	private String id;
	
	private String name;
	
	private Integer age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}

web层

package com.user.controll;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.user.domain.User;

@RestController
@RequestMapping("/user")
public class UserControll {
	
	@GetMapping
	public List<User> findAll() {
		List<User> users = new ArrayList<>();
		User user = new User();
		user.setId("1");
		user.setName("zhangsan");
		user.setAge(18);
		users.add(user);
		return users;
	}
	
	@GetMapping("/{id}")
	public User getUserInfo(String id) {
		User user = new User();
		user.setId("2");
		user.setName("lisi");
		user.setAge(18);
		return user;
	}

}

编写测试类

新建一个测试父类TestParent

package com.user;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestParent {
	
	@Autowired
	protected WebApplicationContext wac;
	
	//MVC环境 
	protected MockMvc mockMvc;
	
	@Before
	public void setup() {
		mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
	}

}

UserControllTest 测试类

package com.user.controll;

import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import com.user.TestParent;

public class UserControllTest extends TestParent {
	
	@Test
	public void whenQuerySuccess() throws Exception {
		mockMvc.perform(MockMvcRequestBuilders.get("/user")
				.param("username", "zhangsan")
				.contentType(MediaType.APPLICATION_JSON_UTF8))
				.andExpect(MockMvcResultMatchers.status().isOk())
				.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(1));
	}
	
	@Test
	public void whenGetInfoSuccess() throws Exception {
		mockMvc.perform(MockMvcRequestBuilders.get("/user/2")
				.contentType(MediaType.APPLICATION_JSON_UTF8))
				.andExpect(MockMvcResultMatchers.status().isOk())
				.andExpect(MockMvcResultMatchers.jsonPath("$.name").value("lisi"));
	}
	
}

测试结果

方法上Run As -> JUnit Test则测试方法
类空白处上Run As -> JUnit Test则测试该测试类的所有@Test标注的方法
包上Run As -> JUnit Test则测试该包下所有测试类
在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!