介绍

概述

  在开发过程中,我们经常会一股脑的写各种业务逻辑,经常等全部大功告成的时候,打个jar包放环境里跑跑看看能不能通,殊不知在各个业务方法中已经漏洞百出,修复一个打一个包,再继续修复,这种效率真的太低下。

  所以我们需要借助一些单元测试来将我们写的代码做一些测试,这样保证局部方法正确,最后再打包整体运行将整个流程再串起来就能提高开发试错效率。当然,我们除了单元测试,我们还可以通过main()方法在每个类中进行测试,文中会一带而过。

常用注解

  • @RunWith(SpringRunner.class):测试运行器,作用类
  • @SpringBootTest:SpringBoot测试类,作用类
  • @Test:测试方法,作用方法
  • @Ignore:忽略测试方法,作用方法
  • @BeforeClass:针对所有测试,只执行一次,且必须为static void
  • @Before:初始化方法,执行当前测试类的每个测试方法前执行
  • @After:释放资源,执行当前测试类的每个测试方法后执行
  • @AfterClass:针对所有测试,只执行一次,且必须为static void

作者:hadoop_a9bb

链接:https://www.jianshu.com/p/81fc2c98774f

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

模板

依赖

<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]);
} }

SpringBoot—单元测试模板(controller层和service层)的更多相关文章

  1. DAO层,Service层,Controller层、View层 的分工合作

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  2. [转]DAO层,Service层,Controller层、View层

    来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...

  3. DAO层,Service层,Controller层、View层

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  4. 分层 DAO层,Service层,Controller层、View层

    前部分摘录自:http://blog.csdn.net/zdwzzu2006/article/details/6053006 DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务 ...

  5. DAO层,Service层,Controller层、View层介绍

    来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...

  6. DAO层,Service层,Controller层、View层协同工作机制

    转自 http://www.blogdaren.com/post-2024.html DAO层:DAO层主要是做数据持久层的工 作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计D ...

  7. DAO层,Service层,Controller层、View层、entity层

    1.DAO(mapper)层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就 ...

  8. java中Action层、Service层和Dao层的功能区分

    Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...

  9. 搭建DAO层和Service层代码

    第一部分建立实体和映射文件 1 通过数据库生成的实体,此步骤跳过,关于如何查看生成反向工程实体类查看SSH框架搭建教程-反向工程章节 Tmenu和AbstractorTmenu是按照数据库表反向工程形 ...

随机推荐

  1. CtsSecurityTestCases#ListeningPortsTest定位tcp端口与pid

    CtsSecurityTestCases#ListeningPortsTest定位tcp端口与pid [问题描述] cts失败项 armeabi-v7a CtsSecurityTestCases an ...

  2. 第一个Vue-cli

    第一步下载node.js https://nodejs.org/zh-cn/ 安装成功后 在cmd 输入 node -v 看看能不能打印出来 在cmd 输入 nmp-v 看看能不能打印出来 全局安装 ...

  3. CC2530串口通信

    任何USART双向通信至少需要两个脚:接收数据输入(RX)和发送数据输出(TX). RX:接收数据串行输入.通过采样技术来区别数据和噪音,从而恢复数据. TX :发送数据输出.当发送器被禁止时,输出引 ...

  4. 【Kafka】Flume整合Kafka

    目录 需求 一.Flume下载地址 二.上传解压Flume 三.配置flume.conf 四.启动flume 五.测试整合 需求 实现flume监控某个目录下面的所有文件,然后将文件收集发送到kafk ...

  5. YOLOV4所用到的一些tricks

    原文链接:http://arxiv.org/abs/2004.10934 整体框架        Bag of Freebies(BoF) & Bag  of Specials (BoS) B ...

  6. linux centos7搭建mysql-5.7.29

    1. 下载mysql 1.1  下载地址 https://downloads.mysql.com/archives/community/ 1.2  版本选择 2. 管理组及目录权限 2.1  解压my ...

  7. Vue中如何监听组件的原生事件

    在首页开发中,右下角有一个返回顶部的小箭头,将它单独封装成一个BackTop组件,但是它何时出现需要依赖于首页的滑动,即另外一个Scroll组件.如果直接在BackTop组件里面监听,则需要通过thi ...

  8. spark aggregate函数

    aggregate函数将每个分区里面的元素进行聚合,然后用combine函数将每个分区的结果和初始值(zeroValue)进行combine操作.这个函数最终返回的类型不需要和RDD中元素类型一致. ...

  9. python之Python Eclipse+PyDec下载和安装教程(超级详细)

    Eclipse 是著名的跨平台 IDE 工具,最初 Eclipse 是 IBM 支持开发的免费 Java 开发工具,2001 年 11 月贡献给开源社区,目前它由非盈利软件供应商联盟 Eclipse ...

  10. node的http模块

    node中的几个常用核心模块的api返回的都是eventEmitter的实例,也就是说都继承了on和emit方法,用以监听事件并触发回调来处理事件. http模块处理网络请求通常是创建一个server ...