spring-boot项目的单元测试

参考文章

Spring Boot Junit单元测试

正文

引入maven依赖

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

编写测试代码,详细代码如下:

import xx.SmsClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * Created by hua on 2016/6/29.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = VaccineFrontStartUp.class)// 指定spring-boot的启动类
@ActiveProfiles("de")
public class SMSClientTest {

    private static final Logger logger = LoggerFactory.getLogger("TestSMS");

    @Autowired
    private SmsClient smsClient;

    @Test
    public void testSendSms() {
        logger.info(String.format("smsClient:%s", smsClient));
        String sendResult = smsClient.sendSMS("15600000001", "2016.06.29");
        logger.info(String.format("发送结果为:%s", sendResult));
    }
}

总结

可以看到,对spring-boot项目进行单元测试是件容易的事儿,需要添加spring-boot-starter-test依赖,然后
使用@RunWith@SpringApplicationConfiguration注解,然后引入自己要测试的bean(在示例代码中测试的是一个短信发送的bean),调用指定方法进行测试即可。
使用@ActiveProfiles(“de”)来指定活动的profile


技术越来越易用,要勇于尝试

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值