Springboot 之 单元测试

来源:互联网 发布:ubuntu拷贝文件夹 编辑:程序博客网 时间:2024/04/30 13:12

本文章来自【知识林】

在Springboot开发过程中会经常用到单元测试,相对写Controller而言,单元测试更为简单方便。
本例子的测试主要是通过单元测试的方式实现上一篇文章《Springboot 之 自定义配置文件及读取配置文件》中的测试。

pom.xml中引入Maven依赖包

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-test</artifactId>    <scope>test</scope></dependency>

读取核心配置文件

application.properties配置文件内容如下:

server.port=9090test.msg=Hello World Springboot!
  • 创建测试类

所有测试类均放在src/test目录下,这里创建MyTest.java文件:

package com.zslin;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.core.env.Environment;import org.springframework.test.context.junit4.SpringRunner;/** * Created by 钟述林 393156105@qq.com on 2016/10/18 11:25. */@SpringBootTest@RunWith(SpringRunner.class)public class MyTest {    @Value("${test.msg}")    private String msg;    @Test    public void testCoreConfig() {        System.out.println(msg);    }    @Autowired    private Environment env;    @Test    public void testCoreConfig2() {        System.out.println(env.getProperty("test.msg"));    }}

注意

1、 要让一个普通类变成一个单元测试类只需要在类名上加入@SpringBootTest@RunWith(SpringRunner.class)两个注释即可。
2、 在测试方法上加上@Test注释。

运行testCoreConfigtestCoreConfig2方法后均可得到:Hello World Springboot!

读取自定义配置文件

  • 创建配置文件

resources/config依然创建名为my-web.properties的配置文件,内容如下:

web.name=zslinweb.version=V 1.0web.author=393156105@qq.com
  • 创建配置文件管理类
package com.zslin.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * Created by 钟述林 393156105@qq.com on 2016/10/18 11:44. */@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "web")@Componentpublic class MyConfig {    private String name;    private String version;    private String author;    public String getAuthor() {        return author;    }    public String getName() {        return name;    }    public String getVersion() {        return version;    }    public void setAuthor(String author) {        this.author = author;    }    public void setName(String name) {        this.name = name;    }    public void setVersion(String version) {        this.version = version;    }}

和上一篇文档中的没什么两样。

  • 创建测试类
package com.zslin;import com.zslin.config.MyConfig;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;/** * Created by 钟述林 393156105@qq.com on 2016/10/18 11:44. */@RunWith(SpringRunner.class)@SpringBootTestpublic class MyConfigTest {    @Autowired    private MyConfig myConfig;    @Test    public void testConfig() {        System.out.println("webName: "+myConfig.getName()+                ", webVersion: "+ myConfig.getVersion()+", webAuthor: "+myConfig.getAuthor());    }}

运行testConfig将得到结果:webName: zslin, webVersion: V 1.0, webAuthor: 393156105@qq.com

示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study03

本文章来自【知识林】

0 0
原创粉丝点击