前言:

关于SpringBoot的单元测试,描述一下三种单元测试的方式。

1、约定

单元测试代码写在src/test/java目录下,单元测试类命名为*Test,前缀为要测试的类名。

2.、使用mock方式单元测试

Spring测试框架提供MockMvc对象,可以在不需要客户端-服务端请求的情况下进行MVC测试,完全在服务端这边就可以执行Controller的请求,跟启动了测试服务器一样。

测试开始之前需要建立测试环境,setup方法被@Before修饰。通过MockMvcBuilders工具,使用WebApplicationContext对象作为参数,创建一个MockMvc对象。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)//这里的Application是springboot的启动类名
@WebAppConfiguration
public class StyleControllerTest { @Autowired
private WebApplicationContext context; private MockMvc mockMvc; private ObjectMapper mapper = new ObjectMapper(); @Before
public void setupMockMvc() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
} @Test
public void testSend() throws Exception {
Long id =1l;
//调用接口,传入添加的用户参数
mockMvc.perform(MockMvcRequestBuilders.get("/style/listStyleById")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(id)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andDo(MockMvcResultHandlers.print()); } }

3、使用Feign方式单元测试

以下是Feign接口的单元测试示例,启动项目,可以测试本jar提供的服务,不启动服务,改为远程服务地址,可以测试远程jar提供的服务。其中

@EnableFeignClients(clients = UserControllerTest.UserServiceFeignClient.class)

类似我们实际应用调用相关服务一样。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = UserControllerTest.class)
@Import({ FeignAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class })
@EnableFeignClients(clients = UserControllerTest.UserServiceFeignClient.class)
public class UserControllerTest { @FeignClient(value = "loan-server", url = "http://localhost:9070/")
public interface UserServiceFeignClient extends UserServiceClient {
} @Autowired
private UserServiceFeignClient userServiceFeignClient; @Test
public void getUser() {
User user = userServiceFeignClient.getSDKUserById(1);
System.out.println(user);
}
}

4、使用Http Rest API 单元测试

使用RestTemplate发起GET或POST请求,其中@SpringBootTest这两行注释掉就不启动SpringBoot容器直接进行远程调用测试。

@RunWith(SpringJUnit4ClassRunner.class)
public class LoanControllerTest { private final static String url = "http://localhost:9070/"; private static RestTemplate restTemplate = new RestTemplate(); @Test
public void test(){
ResponseEntity<String> response = restTemplate.exchange(url + "/loan/getLoanById?id=1" ,
HttpMethod.GET,
new HttpEntity(null),
String.class);
System.out.println("result: " + response.getBody());
}
}

SpringBoot项目单元测试的更多相关文章

  1. springboot 项目单元测试

    项目结构如下 1 引入测试的 maven 依赖 <dependency> <groupId>org.springframework.boot</groupId> & ...

  2. SpringBoot项目单元测试不经过过滤器问题

    SpringBoot使用MockMvc:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-featu ...

  3. SpringBoot系列: 单元测试

    SpringBoot 项目单元测试也很方便, Web项目中单元测试应该覆盖:1. Service 层2. Controller 层 本文前半部分讲解是一些测试基础配置. 对于Service和Contr ...

  4. IDEA中SpringBoot项目快速创建单元测试

    如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...

  5. 【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解

    ========================4.Springboot2.0单元测试进阶实战和自定义异常处理 ============================== 1.@SpringBoot ...

  6. SpringBoot项目创建与单元测试

    前言   Spring Boot 设计之初就是为了用最少的配置,以最快的速度来启动和运行 Spring 项目.Spring Boot使用特定的配置来构建生产就绪型的项目. Hello World 可以 ...

  7. SpringBoot系列: 单元测试2

    之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...

  8. SpringBoot进行单元测试

    SpringBoot进行单元测试,需要在maven中加入以下依赖 <dependency> <groupId>org.springframework.boot</grou ...

  9. ABAP和Java SpringBoot的单元测试

    ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...

随机推荐

  1. Linux - 7种运行级别

    目录:etc/rc.d/init.d 1. linux开机过程 2. 运行级别(0-6) 存储位置 etc/inittab,开机加载,也可以用命令init [数字]切换. # 0 - 停机(默认时为0 ...

  2. 移动端解决悬浮层(悬浮header、footer)会遮挡住内容的方法

    固定Footer Bootstrap框架提供了两种固定导航条的方式: ☑  .navbar-fixed-top:导航条固定在浏览器窗口顶部 ☑  .navbar-fixed-bottom:导航条固定在 ...

  3. SWUST OJ(957)

    逆置单链表 #include <stdio.h> #include <stdlib.h> typedef struct LNode { char data; struct LN ...

  4. xxx征集系统项目目标文档

    分组:每四人一组 主题:xxx征集系统 成果: 讨论结束后,每组提交一份课堂讨论记录(电子版发表到博客上,纸质版小组成员签名,下节课提交). 每人根据课堂讨论结果提交一份系统利益相关者描述案例.撰写项 ...

  5. event对象的clientX,offsetX,screenX,pageX

    chrome: e.pageX——相对整个页面的坐标 e.layerX——相对当前坐标系的border左上角开始的坐标 e.offsetX——相对当前坐标系的border左上角开始的坐标 e.clie ...

  6. Eclipse+Spring boot开发教程

    一.安装 其实spring boot官方已经提供了用于开发spring boot的定制版eclipse(STS,Spring Tool Suite)直接下载使用即可,但考虑到可能有些小伙伴不想又多装个 ...

  7. 深入理解vue-router之keep-alive

    keep-alive 简介 keep-alive 是 Vue 内置的一个组件,可以使被包含的组件保留状态,或避免重新渲染. 用法也很简单: ? 1 2 3 4 5 <keep-alive> ...

  8. Java框架部分---面试题

    说说Spring? Spring的核心是控制反转.依赖注入,Aop(面向切面)相当于把每个bean与bean之间的关系交给第 三方容器进行管理. 说SpringIOC.SpringAOP? Sprin ...

  9. 在Linux终端安装Julia

    官方参考文档:https://julialang.org/downloads/platform.html#generic-binaries 一.centos终端安装 打开Linux终端输入 sudo ...

  10. Binary Tree Path Sum

    Given a binary tree, find all paths that sum of the nodes in the path equals to a given number targe ...