SpringBoot 测试基类

每次写单元测试都要重复写一些方法、注解等,这里我写了一下测试的基类

文章目录


在这里插入图片描述

基类

BaseApplicationTests.java测试基类,其他测试类继承此类即可。

package com.leigq.www.shiro.base;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class BaseApplicationTests {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    private Long time;

    @Before
    public void setUp() {
        this.time = System.currentTimeMillis();
        log.info("==> 测试开始执行 <==");
    }

    @After
    public void tearDown() {
        log.info("==> 测试执行完成,耗时:{} ms <==", System.currentTimeMillis() - this.time);
    }
}

测试

ShiroApplicationTests.java 基类使用测试

package com.leigq.www.shiro.test;

import com.leigq.www.shiro.base.BaseApplicationTests;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

public class ShiroApplicationTests extends BaseApplicationTests {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Test
    public void contextLoads() {
        // 测试时候可以正确获取 DataSourceProperties bean
        log.warn("DriverClassName is {}", dataSourceProperties.getDriverClassName());
    }

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值