创建测试类:
package com.ezerbelcn.spring_boot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @Copyright (C), 2015-2020, EzAnim
* @ClassName: Person
* @Author: Farther
* @E-mail: 2232549876@qq.com
* @Date: 2020/6/29 19:20
* @Version: V1.0
* #configuration-metadata-annotation-processor
* @Description: None
*/
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastname;
private Integer age;
private Boolean alive;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Pet pet;
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getAlive() {
return alive;
}
public void setAlive(Boolean alive) {
this.alive = alive;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
@Override
public String toString() {
return "Person{" +
"lastname='" + lastname + '\'' +
", age=" + age +
", alive=" + alive +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", pet=" + pet +
'}';
}
}
package com.ezerbelcn.spring_boot.bean;
/**
* @Copyright (C), 2015-2020, EzAnim
* @ClassName: Dog
* @Author: Farther
* @E-mail: 2232549876@qq.com
* @Date: 2020/6/29 19:22
* @Version: V1.0
* @Description: None
*/
public class Dog implements Pet {
private String name;
private Integer age;
public Dog() {
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
yml配置文件:
注意空格,空格,空格 !!!
冒号后面都要加空格,lists 的元素 - 符号 后面也要加空格!
增加@ConfigurationProperties注解
prefix指定配置文件中,该配置信息的前缀名
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
只有这个组件是容器中的组件,容器才能提供的@ConfigurationProperties的功能
在pom.xml中增加依赖,启用yml:
右上角的连接点过去之后404,于是只能自己从官网找连接:
https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor
copy官网中的依赖到pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
配置后会提示Re-run Spring Boot 注解配置处理器
重新运行一下SpringBoot的main方法即可!
利用Spring进行单元测
在pom.xml中添加Spring测试依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
使用@RunWith(SpringRunner.class),测试不再使用默认的Junit,而是利用Spring的测试驱动器。
开始测试
直接person为null,输出窗口会有提示:
connected to the target VM,address:'127.0.0.1:25678',transport:'socket'
pom中轻易不要写版本号!!!
这是为什么?答案是,在配置pom.xml文件时不要指定版本号。
由SpringBoot决定使用哪个版本,否则容易出现类似错误!
(这种最可怕,因为他不报错!!!就是效果不对,我找了好久原因! 天呐!!!
)
移除配置文件中的测试项相关依赖项的版本号:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>