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 ...
随机推荐
- soapUI系列之—-06 testrunner实现自动化测试
TestRunner为soapUI自带------testrunner.bat/testrunner.sh 实现步骤: 1. 使用soapUI,针对接口文件创建测试用例. 2. 将测试用例保存至本地, ...
- 阿里云Ubuntu部署java web(1) - 系统配置
系统版本号:ubuntu 12.04 64位 ssh链接服务器(使用终端远程链接): ssh -l username IP地址 假设出现相似例如以下错误: @ WARNING: REMOTE H ...
- iOS7获取UUID以及转换MD5
近期项目开发,运用到要获取UUID转MD5,可是iOS7不能使用获取的UDID的接口(涉及到隐私),获取MAC地址的方式的接口在iOS7下也废弃了.眼下可能的就是获取UUID了,可是在iOS7下,UU ...
- BZOJ 3550 ONTAK2010 Vacation 单纯形
题目大意:给定一个长度为3n的区间.要求选一些数,且随意一段长度为n的区间内最多选k个数.求选择数的和的最大值 单纯形直接搞 注意一个数仅仅能被选一次 因此要加上xi<=1这个约束条件 不明确3 ...
- Java数据类型的分类
java支持的类型分为两类:基本类型和引用类型 一.基本类型 4类8种: (1)整型:int.short.long.byte. (2)浮点型:float.double. (3)字符型:char. (4 ...
- list if else 遍历 特征合并
特征合并 import re l = ['a', 'b1'] ll = [i if re.search('\d', i) is None else i[0:re.search('\d', i).end ...
- java集合的关系
在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便.这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结 ...
- haproxy tcp 反向代理
配置如下: global log 127.0.0.1 local3 warning nbproc 1 maxconn 65535 daemon defaults log global option d ...
- [原创] [C#] 转换Excel数字列号为字母列号
转换Excel数字列号为字母列号 例如: 0 -> A 26 -> AA private static string GetColumnChar(int col) { ; ; ) ) + ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...