整合JUnit 测试
代码测试是开发工作中一个非常重要的环节,之前课程中学习了使用Junit进行代码测试的方法。创建Spring Boot项目时,默认会为项目添加一个名为spring-boot-starter-test的依赖,此依赖会为项目引入相关的测试组件。这些组件支持多种测试框架,如:JUnit、AssertJ、Hamcrest 等
演示案例:
在IDEA中创建Spring Boot项目,实现两位数的加法并使用JUnit对该方法进行测试
用于测试类的注解
@RunWith(SpringRunner.class):使@SpringBootTest 注解生效
@SpringBootTest:创建Spring 的 ApplicationContext,并将其注入到测试类中
@Before、@Test、@After等
最后提示: 虽然 idea生成的测试类,只自动生成了一个@SpringBootTest注解;没有@RunWith注解;也是能够运行的;
但是建议:标准测试类里还是要有@RunWith的,作用是告诉java你这个类通过用什么运行环境运行,例如启动和创建spring的应用上下文。否则你需要为此在启动时写一堆的环境配置代码。你在IDEA里去掉@RunWith仍然能跑是因为在IDEA里识别为一个JUNIT的运行环境,相当于就是一个自识别的RUNWITH环境配置。但在其他IDE里并没有。所以,为了你的代码能在其他IDE里边正常跑,建议还是加@RunWith
public class Addition {
//实现两个数的加法
public int add(int a,int b){
int m=a+b;
return m;
}
}
import com.msds.demo.Addition;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)//使@SpringBootTest注解生效 加载Springboot测试
@SpringBootTest//创建Spring的ApplicationContext,并将其注入到测试类中
class Ch031junitApplicationTests {
@Test
void contextLoads() {
Addition addition=new Addition();
int temp=addition.add(1,1);
System.out.println(temp);
Assert.assertEquals("计算的结果正确!",temp,2);
}
}
读取配置文件
SpringBoot使用一个全局的配置文件,配置文件名是固定的 (必须是下面的);
application.properties
application.yml
在Web项目中,经常需要从properties文件获取配置信息。在Spring项目中可以通过@PropertySource和@Value注解实现该功能。而在Spring Boot项目中使用@Value和@ConfigurationProperties注解,即可获取application.properties或application.yml文件中的配置信息。
YAML语法
– 使用缩进表示层级关系
– 缩进时不允许使用Tab键,只允许使用空格。
– 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
– 大小写敏感
1.YAML——基本语法
k:(空格)v:表示一对键值对(空格必须要有)
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
server:
port: 8082
属性和值也是大小写敏感
YAML支持的三种数据结构
– 字面量:单个的、不可再分的值
– 对象、map:键值对的集合
– 数组、list和set集合:一组按次序排列的值
2.YAML——值的写法
2.1字面量:普通的值(数字、字符串、布尔):
k: v 字面直接来写
字符串默认不用加上单引号或者双引号;
“”:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
’‘:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
2.2对象、Map(属性和值)(键值对):
k: v 在下一行来写对象的属性和值的关系;注意缩进
对象还是k: v的方式
friends:
lastName: zhangsan
age: 18
########################################################
对象名:
属性名: 属性值
属性名: 属性值
########################################################
map集合名:
key: value
key: value
行内写法
friends: {lastName: zhangsan,age: 18} ######################################################### 对象名: {属性名: 属性值,属性名: 属性值...} ######################################################### friends: {key: value,key: value...}
2.3数组、集合(List、Set):
用-(空格)值表示数组中的一个元素
pets: - cat - dog - pig ######################################################### 数组名: - 值1 - 值2 - 值3 ######################################################### list或set集合名: - 值1 - 值2 - 值3
行内写法
pets: [cat,dog,pig] ######################################################### 数组名: [值1,值2,值3...] ######################################################### list或set集合名: [值1,值2,值3...]
配置文件值得注入
属性名匹配规则(Relaxed binding)
– user.userName:使用标准方式
– user.user-name:大写用-
– user.user_name:大写用_
演示案例:
配置文件
user12:
#userName: "张\n三"
userName: '张\n三'
age: 18
boss: true
brith: 2020/10/1
#maps: {key01: one,key02: two}
maps:
key01: one
key02: two
#lists: [list01,list02,list03]
lists:
- list01
- list02
- list03
#dog: {name: 欢欢, age: 6}
dog:
name: 欢欢
age: 6
#str: [str01,str02,str03]
str:
- str01
- str02
- str03
JavaBean
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix = "user12",配置文件中那个下面的所有属性进行一一映射,
YAML文件中的属性一定要和Bean的属性进行关联
* 只有这个组件是容器中的组件,才能使用容器提供的功能 添加@Component
* @ConfigurationProperties(prefix = "user12")默认从全局配置文件中获取值
* @ConfigurationProperties注解实际上是通过属性的setter方法来进行注入,因此要获取配置信息的相关属性
* 需要在其所在的Bean类中创建该属性的setter方法
*/
@Component
@ConfigurationProperties(prefix = "user12")
public class User {
private String userName;//用户姓名
private Integer age;//用户年龄
private boolean boss;//用户老板
private Date brith;//用户生日
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
private String[] str;
//此处省略 getter and setter toString()
}
public class Dog {
private String name;
private Integer age;
//此处省略 getter and setter
}
我们可以导入配置文件处理器,以后编写配置就有提示了,在pom.xml中进行配置
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
测试类
@Autowired
private User user;
@Test