导入Maven

        
org.springframework.boot
spring-boot-starter-test
test

这里要单元测试的是打印出实体类的信息,而实体类的信息在.yml文件中
文件夹:
SpringBoot的单元测试_单元测试

实体类
Person.java

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean boss;
private Date date;
private Map maps;
private List lists;
private Dog dog;

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", boss=" + boss +
", date=" + date +
", maps=" + maps +
", list=" + lists +
", dog=" + dog +
'}';
}

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;
}

public Boolean getBoss() {
return boss;
}

public void setBoss(Boolean boss) {
this.boss = boss;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}


public Map getMaps() {
return maps;
}

public void setMaps(Map maps) {
this.maps = maps;
}

public List getLists() {
return lists;
}

public void setLists(List lists) {
this.lists = lists;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}
}

其中有Dog的属性
所以
Dog.java

public class Dog {
private String name;
private String color;



@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
}

application.yml

person:
name: zhangsan
age: 20
boss: false
date: 2020/11/22
maps: {k1: "1",k2: "2"}
lists:
- lisi
- wangwu
dog:
name: xiao
color: white

最后在test下面的java包中写测试类

@RunWith(SpringRunner.class)//用Spring的驱动器跑
@SpringBootTest//SpringBoot的单元测试
public class Test01 {
@Autowired
Person person;
@Test
public void test1(){
System.out.println(person);
}

}

这里应用的为SpringBoot的单元测试,用的是Spring的驱动器进行测试

输出结果
SpringBoot的单元测试_单元测试_02