IDEA中SpringBoot项目快速创建单元测试
如何在IDEA中对于SpringBoot项目快速创建单元测试
创建测试用例

右键需要进行测试的方法,选择GO TO然后选择Test

点击Create New Test

勾选需要创建单元测试的方法
然后点击OK就直接创建完成了。
修改测试用例
在类上面加上注解
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
然后在下面注入需要测试的类然后在方法里面使用,使用方法和普通的单元测试一样
如果测试的是service
demo如下
package com.example.demo; 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.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class HelloServiceTest { @Autowired
private HelloService helloService; @Test
public void hello(){
helloService.hello();
} }
如果测试的是controller
需要加入@AutoConfigureMockMvc的注解
那么demo如下
package com.example.demo; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest { @Autowired
private MockMvc mockMvc; @Test
public void hello() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/hello"))
.andExpect(MockMvcResultMatchers.status().isOk());
} }
其中对于MockMvc的使用可以自己研究一下,里面有很多测试使用的东西,这边只是举个例子,只要访问是200不是404这种错误都会过测试用例
IDEA中SpringBoot项目快速创建单元测试的更多相关文章
- SpringBoot项目快速启动停止脚本
SpringBoot项目快速启动停止脚本 1.在jar包同级目录下,创建 app.sh #!/bin/bash appName=`ls|grep .jar$` if [ -z $appName ] t ...
- JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...
- springBoot(2)---快速创建项目,初解jackson
快速创建项目,初解jackson 一.快速创建项目 springboot官网提供了工具类自动创建web应用:网址:http://start.spring.io/ 官网页面 1.快速创建一个 选择web ...
- SpringBoot项目的创建流程(初步)
小伙伴们在学习的过程中大概也发现了这两个框架需要我们手动配置的地方非常多,不过做JavaEE开发的小伙伴们肯定也听说过“约定大于配置”这样一句话,就是说系统,类库,框架应该假定合理的默认值,而非要求提 ...
- springboot项目快速搭建
1. 问题描述 springboot的面世,成为Java开发者的一大福音,大大提升了开发的效率,其实springboot只是在maven的基础上,对已有的maven gav进行了封装而已,今天用最简单 ...
- springboot项目快速构建
1. 问题描述 springboot的面世,成为Java开发者的一大福音,大大提升了开发的效率,其实springboot只是在maven的基础上,对已有的maven gav进行了封装而已,今天用最简单 ...
- springboot项目的创建
创建springboot项目 包名和项目名 选择需要使用的框架,web 然后再点击下一步,完成即可创建springboot项目
- 在linux中部署项目并创建shell脚本
1.首先要在idea中父工程maven包下执行clean生成的target包 2.执行package打包,打包时候讲test勾去掉 3.将target包中生成的jar包cp出来 此处注意打包时必须要保 ...
- IDEA中springboot项目添加yml格式配置文件
1.先创建application.properties 文件,在resources文件夹,右键 new -> Resource Bundle 如下图所示,填写名称 2.生成如下图所示文件 3. ...
随机推荐
- 2017-2018-2 20165315 实验四《Android程序设计》实验报告
2017-2018-2 20165315 实验四<Android程序设计>实验报告 第24章:初识Android Android Studio项目的目录树 1 build:该目录包含了自动 ...
- Job for ssh.service failed because the control process exited with error code.......
转载自:https://blog.csdn.net/woailyoo0000/article/details/79782986 笔者最近更新ubuntu系统,在更新之前设置了证书信任文件,重启以后ss ...
- git连接远程客户端,命令行窗口上传文件
1.git官网,下载安装git客户端 2.配置全局的name和email,生成key git config --global user.name XXX git config --global us ...
- Log4Net web.config配置
1 .[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)] 写 ...
- 【APP测试(Android)】--性能测试
- for循环的实例
1.大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,该如//首先我们要知道一百石粮食需要这些马分别几匹 //第一个是大马,需要五十匹马for(var x=0;x&l ...
- tensorflow学习之(五)构造简单神经网络 并展示拟合过程
# def 添加层 如何构造神经网络 并展示拟合过程 import tensorflow as tf import numpy as np import matplotlib.pyplot as pl ...
- Spring的概念
一.思想 IOC: DI: 二.applicationContext&BeanFactory
- ios 导航push跳转方向设置
CATransition* transition = [CATransition animation]; transition.type = kCATransitionPush;//可更改为其他方式 ...
- Python PEP 8 编码规范中文版
原文链接:http://legacy.python.org/dev/peps/pep-0008/ 转发链接:https://blog.csdn.net/ratsniper/article/detail ...