SpingBoot(12)SpringBoot下基于Junit的单元测试

前言

JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量。

JUnit 测试框架具有以下重要特性:

  • 测试工具
  • 测试套件
  • 测试运行器
  • 测试分类

那为什么要测试呢?

  •  可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 
  •  可以自动测试,可以在项目打包前进行测试校验 
  • 可以及时发现因为修改代码导致新的问题的出现,并及时解决

Junit常用注解:

  • @Before:初始化方法
  • @After:释放资源
  • @Test:测试方法,在这里可以测试期望异常和超时时间
  • @Ignore:忽略的测试方法
  • @BeforeClass:针对所有测试,只执行一次,且必须为static void
  • @AfterClass:针对所有测试,只执行一次,且必须为static void
  • @RunWith:指定使用的单元测试执行类

一个单元测试类执行顺序为:

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass

每一个测试方法的调用顺序为:

@Before –> @Test –> @After

测试service

package com.springboot.springboot.service;

import com.springboot.springboot.utils.JedisAdapter;
import com.springboot.springboot.utils.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author WilsonSong
 * @date 2018/6/2
 * 踩赞功能
 */
@Service
public class LikeService {

    @Autowired
    JedisAdapter jedisAdapter;

    //显示当前有多少人喜欢
    public long getLikeCount(int entityType, int entityId ){
        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);
        return jedisAdapter.scard(likeKey);
    }

    //显示某个用户对某个内容是点赞还是点踩
    public int getLikeStatus(int userID, int entityType, int entityId){
        //点了赞
        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);
        if (jedisAdapter.sismember(likeKey,String.valueOf(userID))){
            return 1;
        }

        //-1的话点了踩, 0 的话什么都不是
        String dislikeKey = RedisKeyUtil.getDislikeKey(entityType,entityId);
        return jedisAdapter.sismember(dislikeKey,String.valueOf(userID))? -1 : 0;
    }

    //点赞
    public long like(int userID, int entityType, int entityId){
        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);
        jedisAdapter.sadd(likeKey,String.valueOf(userID));

        //因为一个人点完赞不可能再点踩,所以把点踩去掉
        String dislikeKey = RedisKeyUtil.getDislikeKey(entityType,entityId);
        jedisAdapter.srem(dislikeKey,String.valueOf(userID));

        return jedisAdapter.scard(likeKey);
    }

    //点踩
    public long dislike(int userID, int entityType, int entityId){
        String dislikeKey = RedisKeyUtil.getDislikeKey(entityType,entityId);
        jedisAdapter.sadd(dislikeKey,String.valueOf(userID));

        //因为一个人点完赞不可能再点踩,所以把点踩去掉
        String likeKey = RedisKeyUtil.getLikeKey(entityType,entityId);
        jedisAdapter.srem(likeKey,String.valueOf(userID));

        return jedisAdapter.scard(likeKey);
    }



}

测试类

package com.springboot.springboot;

import com.springboot.springboot.service.LikeService;
import org.junit.*;
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.SpringJUnit4ClassRunner;

/**
 * 单元测试
 * @author WilsonSong
 * @date 2018/7/26
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringbootDemo03Application.class)  // 指定spring-boot的启动类
public class LikeServiceTests {
    @Autowired
    LikeService likeService;

//    测试需要准备的数据初始化
    @Before
    public void setUp(){
        System.out.println("before");
    }

//    测试可能增加了一些数据,需要在这里把这些数据清理
    @After
    public void tearDown(){
        System.out.println("after");
    }

    //必须是static的
    @BeforeClass
    public static void beforeClass() {
        System.out.println("beforeClass");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("afterClass");
    }

    @Test
    public void testLike(){
        System.out.println("LikeTest");
        likeService.like(123,1,1);
        Assert.assertEquals(1,likeService.getLikeStatus(123,1,1));

        likeService.dislike(123,1,1);
        Assert.assertEquals(-1,likeService.getLikeStatus(123,1,1));
    }

    //期待抛出一些异常
    @Test(expected = IllegalArgumentException.class)
    public void testException(){
        System.out.println("testException");
        throw new IllegalArgumentException("发生异常");

    }
}

更加具体的可以参照

https://blog.csdn.net/qq_35915384/article/details/80227297

猜你喜欢

转载自blog.csdn.net/WilsonSong1024/article/details/81216268