码迷,mamicode.com
首页 > 编程语言 > 详细

springboot项目进行单元测试

时间:2019-01-16 12:00:30      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:bean   text   app   imp   pen   with   art   system   unit   

使用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项目进行单元测试

标签:bean   text   app   imp   pen   with   art   system   unit   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!