SpringBoot—单元测试模板(controller层和service层)
介绍
概述
在开发过程中,我们经常会一股脑的写各种业务逻辑,经常等全部大功告成的时候,打个jar包放环境里跑跑看看能不能通,殊不知在各个业务方法中已经漏洞百出,修复一个打一个包,再继续修复,这种效率真的太低下。
所以我们需要借助一些单元测试来将我们写的代码做一些测试,这样保证局部方法正确,最后再打包整体运行将整个流程再串起来就能提高开发试错效率。当然,我们除了单元测试,我们还可以通过main()
方法在每个类中进行测试,文中会一带而过。
常用注解
@RunWith(SpringRunner.class)
:测试运行器,作用类@SpringBootTest
:SpringBoot测试类,作用类@Test
:测试方法,作用方法@Ignore
:忽略测试方法,作用方法@BeforeClass
:针对所有测试,只执行一次,且必须为static void@Before
:初始化方法,执行当前测试类的每个测试方法前执行@After
:释放资源,执行当前测试类的每个测试方法后执行@AfterClass
:针对所有测试,只执行一次,且必须为static void
模板
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
spring-boot-starter-test
内部包含了junit
功能。
controller层单元测试模板
controller层示例
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Andya
* @create 2020-04-22 22:41
*/
@RestController
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "hello";
}
}
controller层单元测试类
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Andya
* @create 2020-06-02 13:59
*/
public class HelloControllerTest {
private MockMvc mockMvc;
@Before
public void setUp() throws Exception{
mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@Test
public void getHello() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
service层单元测试模板
service层示例
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @author Andya
* @create 2020-06-01 9:30
*/
@Service
public class TwoSum {
public static int[] twoSum1(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++){
map.put(nums[i], i);
}
System.out.println(map);
for (int i = 0; i < nums.length; i++) {
int result = target - nums[i];
if(map.containsKey(result) && map.get(result) != i) {
return new int[] {i,map.get(result)};
}
}
throw new IllegalArgumentException("No two sum solution");
}
public static int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++){
for(int j = i+1; j < nums.length;j++) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return null;
}
public static int[] twoSum2(int[] nums, int target){
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
//计算结果
int result = target - nums[i];
//map中是否包含这个结果,若包含则返回该结果,及对应的目前数组的index
if (map.containsKey(result)) {
//map是后添加元素的,所以map索引在前,i在后
return new int[]{map.get(result), i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("no two sum solution");
}
}
service层单元测试类
import com.example.andya.demo.service.algorithm.TwoSum;
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.test.context.junit4.SpringRunner;
/**
* @author Andya
* @create 2020-06-02 14:36
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TwoSumTest {
@Autowired
TwoSum twoSum;
@Test
public void testSum1(){
int[] nums = new int[] {1,1,3};
int[] newNums = twoSum.twoSum1(nums,2);
System.out.println(newNums[0] + ":" +newNums[1]);
}
@Test
@Ignore
public void testSum2() {
int[] nums = new int[] {2,2,4};
int[] newNums = twoSum.twoSum2(nums,4);
System.out.println(newNums[0] + ":" +newNums[1]);
}
}
烧不死的鸟就是凤凰