SpringBoot 2.x (4):配置文件与单元测试
SpringBoot的配置文件有默认的application.properties
还可以使用YAML
区别:
application.properties示例:
server.port=8090
server.session-timeout=30
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8
application.yml示例:
server:
port: 8090
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8
两种方式都优于SSM的XML配置形式,个人更偏向于使用properties文件
SpringBoot默认的配置项:
这里不多写了,可以去Spring官网查找
也可以参考一位大佬的博客:
https://www.cnblogs.com/SimpleWu/p/10025314.html
如何在代码中读取配置文件中的自定义信息呢?
比如:
file.path=D:\\temp\\images\\
想要在代码中获取这个值的话:
@Value("${file.path}")
private String FILE_PATH;
将配置文件映射到实体类:
第一种方式:手动给实体类属性注入
配置文件:
test.name=springboot
test.domain=www.dreamtech.org
实体类:
package org.dreamtech.springboot.domain; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class ServerSettings {
@Value("${test.name}")
private String name;
@Value("${test.domain}")
private String domain; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDomain() {
return domain;
} public void setDomain(String domain) {
this.domain = domain;
} }
Controller:
@Autowired
private ServerSettings serverSettings; @RequestMapping("/test")
@ResponseBody
private ServerSettings test() {
return serverSettings;
}
第二种方式:根据前缀自动注入
实体类:
package org.dreamtech.springboot.domain; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "test")
public class ServerSettings {
private String name;
private String domain; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDomain() {
return domain;
} public void setDomain(String domain) {
this.domain = domain;
} }
注意:如果使用了前缀的方式,那么不能再使用@Value注解了!
下面进行SpringBoot测试学习:
普通的单元测试:
首先导入依赖:通常SpringBoot新项目带有这个依赖,如果没有,自行导入即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
在test目录下新建测试类:
@Test用于测试;@Before测试前运行;@After测试后运行
package org.dreamtech.springboot; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import junit.framework.TestCase; @RunWith(SpringRunner.class)
@SpringBootTest(classes = { SpringbootApplication.class })
public class SpringBootTestDemo {
@Test
public void testOne() {
System.out.println("hello world!");
TestCase.assertEquals(1, 1);
} @Before
public void testBefore() {
System.out.println("Before");
} @After
public void testAfter() {
System.out.println("After");
}
}
对API接口进行测试:使用MockMVC
MockMVC相当于就是一个HTTP客户端,模拟用户使用浏览器进行操作
写一个接口进行测试:
@RequestMapping("/test")
@ResponseBody
private String test() {
return "test";
}
测试类:
package org.dreamtech.springboot; 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.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import junit.framework.TestCase; @RunWith(SpringRunner.class)
@SpringBootTest(classes = { SpringbootApplication.class })
@AutoConfigureMockMvc
public class MockMvcTest {
@Autowired
private MockMvc mockMvc; @Test
public void apiTest() throws Exception {
// 以GET方式访问/test路径,如果返回是200状态码说明调用成功
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/test"))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
TestCase.assertEquals(mvcResult.getResponse().getStatus(), 200);
}
}
最后记录一点有趣的东西:
启动的时候显示SpringBoot多单调啊,不如找一些好看的!
在classpath下面写一个banner.txt:
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
////////////////////////////////////////////////////////////////////
Spring Boot Version: ${spring-boot.version}
SpringBoot 2.x (4):配置文件与单元测试的更多相关文章
- springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试
包结构 所需要的jar包直接拷贝到lib目录下 然后选定 build path 之后开始写项目代码 配置文件 ApplicationContext.xml <?xml version=" ...
- SpringBoot第二篇:配置文件详解一
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10837594.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 Sprin ...
- Springboot学习:核心配置文件
核心配置文件介绍 SpringBoot使用一个全局配置文件,配置文件名是固定的 application.properties application.yml 配置文件的作用:修改SpringBoot自 ...
- 从SpringBoot源码分析 配置文件的加载原理和优先级
本文从SpringBoot源码分析 配置文件的加载原理和配置文件的优先级 跟入源码之前,先提一个问题: SpringBoot 既可以加载指定目录下的配置文件获取配置项,也可以通过启动参数( ...
- springboot整合H2内存数据库,实现单元测试与数据库无关性
一.新建spring boot工程 新建工程的时候,需要加入JPA,H2依赖 二.工程结构 pom文件依赖如下: <?xml version="1.0" encoding ...
- springboot学习二:配置文件配置
springboot默认读取application*.properties #######spring配置####### spring.profiles.active=dev //引入开发配置文件 a ...
- springboot下整合各种配置文件
本博是在springboot下整合其他中间件,比如,mq,redis,durid,日志...等等 以后遇到再更.springboot真是太便捷了,让我们赶紧涌入到springboot的怀抱吧. ap ...
- SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)
实现项目的多环境配置的方法有很多,比如通过在Pom.xml中配置profiles(最常见) 然后在Install项目打War包的时候,根据需求打不同环境的包,如图: 这种配置多环境的方法在SSM框架中 ...
- JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理
转载 https://www.cnblogs.com/xieyupeng/p/9664104.html @Value获取值和@ConfigurationProperties获取值比较 @Confi ...
随机推荐
- 【转】Selenium2学习路线
课程大纲:第一部分:基础入门 第一课: SELENIUM2的原理介绍及环境搭建本节课主要讲解SELENIUM2的原理,让大家了解SELENIUM2的发展历程,同时解惑大家对自动化测试中产生的一些误区. ...
- 【HRS项目】Axure兴许问题解决---与SVN结合
上一篇博客介绍了Axure的团队开发用法,http://blog.csdn.net/u013036274/article/details/50999139,可是再用的时候发现会出现这种问题,例如以下图 ...
- 深度学习笔记之使用Faster-Rcnn进行目标检测 (实践篇)
实验 我使用的代码是Python版本的Faster Rcnn,官方也有Matlab版本的,链接如下: py-faster-rcnn(python) faster-rcnn(matlab) 环境配置 按 ...
- 常见网络摄像机默认使用的端口,RTSP地址
品牌 默认IP地址 WEB RTSP HTTPS 数据 ONVIF 海康威视 192.168.1.64/DHCP用户名admin 密码自己设 80 554 443 8000 80 大华 192 ...
- 屏蔽微软的SignalR
去年采用ASP.NET MVC开发项目,在谷歌浏览器里调试页面的时候,发现项目在不停地请求数据,链接很奇怪: http://localhost:63004/654c2dd725bb4401b8fc0c ...
- 1分钟看完 jQuery UI
jQuery UI简介 jQuery UI包含了许多维持状态的小部件(Widget),因此,它与典型的 jQuery 插件使用模式略有不同.所有的 jQuery UI 小部件(Widget)使用相同的 ...
- ps -ef | grep
ps -ef | grep java 查看所有关于java的进程 root 17540 1 0 2009 ? 01:42:27 /usr/java/jdk1.5. ...
- Serializable and XmlEnum
The easiest way is to use [XmlEnum] attribute like so: [Serializable] public enum EnumToSerialize { ...
- DWR(Direct Web Remoting)是什么
DWR可以用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助你开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一 ...
- bzoj1296 [SCOI2009]粉刷匠——背包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1296 对于不同木板之间,最终统计答案时做一个分组背包即可: 而要进行分组背包,就需要知道每个 ...