springboot项目进行单元测试

使用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 即可。

猜你喜欢

转载自www.cnblogs.com/hibugs/p/10275539.html