首页 > 编程语言 > 详细

springboot项目进行单元测试

时间:2019-01-16 10:45:39      阅读:199      评论:0      收藏:0      [点我收藏+]

使用springboot开发项目时,通过简单的注解可以方便地单元测试,方式如下:

一、引入springboot-test依赖

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

二、在src/test/java目录下创建测试类

由于我们在上一步引入dependence的时候指定了scope为test,所以只能在test目录下创建测试类。

package com.test;

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;

import com.winterchen.Springboot2MybatisDemoApplication;
import com.zxy.demo.redis.RedisUtil;

@SpringBootTest(classes = Springboot2MybatisDemoApplication.class)
@RunWith(SpringRunner.class)
public class RedisTest {

    @Autowired
    RedisUtil r;

    @Test
    public void reidsTest() {
        r.set("luyiming", "is a programmer");
        System.out.println(r.get("luyiming"));
    }

}

其中@SpringBootTest注解里需要写明springboot的加载类,然后让该测试类 Run As ->Junit Test 即可。

springboot项目进行单元测试

原文:https://www.cnblogs.com/hibugs/p/10275539.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!