当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot 2.x (4):配置文件与单元测试

SpringBoot 2.x (4):配置文件与单元测试

2019年05月06日  | 移动技术网IT编程  | 我要评论

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}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网