springboot 2.0.5.RELEASE单元测试例子

假装没事ソ 提交于 2020-01-22 12:55:09

springboot版本:2.0.5.RELEASE

jdk:1.8

目标,根据一个properties文件获取内容,并添加到对象上。并通过springboot单元测试

在esources根目录下添加other.properties文件

other.properties内容

other.title=burns
other.blog=http://zhongyuele.top/

在com.esoon.ids.entity包中创建OtherProperties类

OtherProperties.java

package com.esoon.ids.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@Data
@ConfigurationProperties(prefix = "other")
@PropertySource(("classpath:other.properties"))
public class OtherProperties {
    private String title;
    private String blog;

}

添加测试类OtherPropertiesTest.java

package com.esoon.ids.entiry;

import com.esoon.ids.Ids20200121Application;
import com.esoon.ids.entity.OtherProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Ids20200121Application.class})
public class OtherPropertiesTest {
    @Autowired
    private OtherProperties op;

    @Test
    public void testOther() throws Exception{
        System.out.println(op.toString());
    }
}

 

输出

OtherProperties(title=burns, blog=http://zhongyuele.top/)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!