Springboot 之 单元测试

本文章来自【知识林】

在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=9090

test.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=zslin
web.version=V 1.0
web.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")
@Component
public 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)
@SpringBootTest
public 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

本文章来自【知识林】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值