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

(1) 记录测试方法运行的时间

(2)两个父类方法 print,可打印list和object对象

(3)一个属性 logger 记录日志

(4)DateUtil 是封装的一个处理时间的工具类

下面是代码:

DateUtil.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* 时间日期格式化
*
* @author lixingwu
*/
public class DateUtil { /**
* <p> 方法描述:毫秒转分钟秒数. </p>
* <p> 创建时间:2017-12-08 14:07:01 </p>
* <p> 创建作者:李兴武 </p>
*
* @param ms 毫秒数
* @return xx分钟xx秒
* @author "lixingwu"
*/
public static String formatTime(Long ms) {
Integer ss = 1000;
Integer mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = ms / dd;
Long hour = (ms - day * dd) / hh;
Long minute = (ms - day * dd - hour * hh) / mi;
Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss; StringBuilder sb = new StringBuilder();
if (day > 0) {
sb.append(day).append("天");
}
if (hour > 0) {
sb.append(hour).append("小时");
}
if (minute > 0) {
sb.append(minute).append("分钟");
}
if (second > 0) {
sb.append(second).append("秒");
}
if (milliSecond > 0) {
sb.append(milliSecond).append("毫秒");
}
return sb.toString();
}
}

BaseSpringBootTest .java

import com.zhwlt.logistics.utils.DateUtil;
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.SpringJUnit4ClassRunner;
import org.springframework.util.CollectionUtils; import java.util.List; /**
* 测试基类
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public abstract class BaseSpringBootTest { protected Logger logger = LoggerFactory.getLogger(this.getClass()); private long time; public long getTime() {
return time;
} public void setTime(long time) {
this.time = time;
} @Before
public void setUp() throws Exception {
this.setTime(System.currentTimeMillis());
logger.info("==> 测试开始执行 <==");
} @After
public void tearDown() throws Exception {
logger.info("==> 测试执行完成,耗时:{} <==",
DateUtil.formatTime(System.currentTimeMillis() - this.getTime()));
} /**
* 方法描述:打印list.
* 创建时间:2018-10-11 00:23:28
*/
<T> void print(List<T> list) {
if (!CollectionUtils.isEmpty(list)) {
list.forEach(System.out::println);
}
} void print(Object o) {
System.out.println(o.toString());
} }

使用方法:

测试类继承 BaseSpringBootTest  即可,例如:

import org.junit.Test;
import javax.annotation.Resource; /**
* 异步任务 测试
*/
public class AsyncServiceTest extends BaseSpringBootTest { @Resource
private AsyncService asyncService; @Test
public void AsyncMTest() {
asyncService.AsyncM();
System.out.println("==> 测试异步任务");
}
}

SpringBoot 测试基类的更多相关文章

  1. python3+selenium框架设计04-封装测试基类

    在完成了日志类封装之后,那我们就要对测试基类进行实现,在其中对一些请求再次封装,在项目下新建一个framework文件夹,在文件夹下新建Base_Page.py文件,这是用来写测试基类的文件.在项目下 ...

  2. 背水一战 Windows 10 (69) - 控件(控件基类): UIElement - Manipulate 手势处理, 路由事件的注册, 路由事件的冒泡, 命中测试的可见性

    [源码下载] 背水一战 Windows 10 (69) - 控件(控件基类): UIElement - Manipulate 手势处理, 路由事件的注册, 路由事件的冒泡, 命中测试的可见性 作者:w ...

  3. SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...

  4. Java如何解决脆弱基类(基类被冻结)问题

    概述  大多数好的设计者象躲避瘟疫一样来避免使用实现继承(extends 关系).实际上80%的代码应该完全用interfaces写,而不是通过extends.“JAVA设计模式”一书详细阐述了怎样用 ...

  5. EF实体框架数据操作基类(转)

    //----------------------------------------------------------------// Copyright (C) 2013 河南禄恒软件科技有限公司 ...

  6. 基类用的this指针

    结论:基类构造函数中的this指针指向的是派生类的对象 测试代码: #include <iostream> using namespace std; class father; fathe ...

  7. Entity Framework 实体框架的形成之旅--为基础类库接口增加单元测试,对基类接口进行正确性校验(10)

    本篇介绍Entity Framework 实体框架的文章已经到了第十篇了,对实体框架的各个分层以及基类的封装管理,已经臻于完善,为了方便对基类接口的正确性校验,以及方便对以后完善或扩展接口进行回归测试 ...

  8. WPF开发时光之痕日记本(二)—— MVVM基类

    当我们用MVVM的时候要实现INotifyPropertyChanged,每次都要实现这个接口比较麻烦,所以基类的作用就体现出来了.代码如下: public class ViewModelBase : ...

  9. [theWord] 一种英文字典的基类设计

    theWord --- 一种英文字典的基类设计 使用场景 想写一个应用,来记录自己背单词时候,对每个单词的记忆状况之类的东西.至于为什么做这个,试过了一些背单词软件,并不觉得好用,自己做一个吧. 那么 ...

随机推荐

  1. 基于S2SH开发学生考勤管理系统 附源码

    开发环境: Windows操作系统开发工具:Eclipse+Jdk+Tomcat+mysql数据库 运行效果图 源码及原文链接:http://javadao.xyz/forum.php?mod=vie ...

  2. Python之write与writelines区别

    一.传入的参数类型要求不同: 1. file.write(str)需要传入一个字符串做为参数,否则会报错. write( "字符串") with open('20200222.tx ...

  3. vue自定义分页组件---切图网

    vue2.5自定义分页组件 Pagination.vue,可设置每页显示条数,带跳转框直接跳转到相应页面,亲测有用.目前很多框架自带有分页组件比如elementUI,不过在面对一个拿到PSD稿,然后重 ...

  4. 4~20MA 转 电压输出

    ICL7660  50mA LM2662/LM2663  200mA

  5. ArcGIS Engine开发碰到问题及解决方式

    1.问题描述——运行提示:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any Arc ...

  6. python3-cookbook笔记:第五章 文件与IO

    python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...

  7. 浅谈python的第三方库——numpy(终)

    本文作为numpy系列的总结篇,继续介绍numpy中常见的使用小贴士 1 手动转换矩阵规格 转换矩阵规格,就是在保持原矩阵的元素数量和内容不变的情况下,改变原矩阵的行列数目.比如,在得到一个5x4的矩 ...

  8. STL入门学习中碰到的一些函数

    2020.02.10 fill #include<algorithm> vector<int> v{ 1, 2, 3, 3 }; fill(v.begin(), v.end() ...

  9. Spring mvc拦截器防御CSRF攻击

    CSRF(具体参考百度百科) CSRF(Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSR ...

  10. 快捷使用 Iterm2 连接SSH ( HTTP代理 )

    1,配置iterm2 > Preferences.. > Profiles > 填写:name : 别名 : Command : expect /Users/jerryxu/wwwr ...