添加依赖到 pom.xml
<!-- JUnit 输出日志用 -->
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.16.0</version>
<scope>test</scope>
</dependency>
先上测试类
BaseTest
是我提取的测试基类。
package com.jerry.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.jerry.BaseTest;
import com.jerry.entity.Book;
import com.jerry.service.IBookService;
public class BookServiceTest extends BaseTest {
private IBookService bookService= getBean(IBookService.class);
public void testGetById() {
Book book= bookService.getById(123L);
assertNotNull(room);
String jsonString = JSONObject.toJSONString(book);
log.out(jsonString );
}
}
方案一【测试类提个基类,基类实现日志输出】
- 方便获取 bean
- 方便输出信息到控制台
package com.jerry;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
/**
* 为了取 bean 方便提个基类。
*/
public class BaseTest extends TestCase {
//正确输出
@Rule
protected final static SystemOutRule out= new SystemOutRule();
//错误输出
@Rule
protected final static SystemErrRule err= new SystemErrRule();
//初始化 spring 上下文;
protected static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
public <T> T getBean(Class<T> requiredType) {
return context.getBean(requiredType);
}
public Object getBean(String requiredType) {
return context.getBean(requiredType);
}
/**
* 用于测试中,向控制台输出信息
* @author jerryjin
*/
protected static class log{
public static void out(String msg){
System.out.print(msg);
out.getLog();
}
public static void err(String msg){
System.err.print(msg);
err.getLog();
}
}
}
方案二【日志功能放到工具类,Test基类引用之】
把日志功能放到工具类中,在测试基类引用日志工具类。这样能避免单继承的限制(因为有的时候,我只需要部分功能,所以根据需要提了几个基类,用于各种情况。)
创建日志工具基类
package com.jerry.tools;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
/**
* JUnit 日志输出工具类
*/
public class JUnitLogTool {
// 正确输出
@Rule
protected final static SystemOutRule logOut = new SystemOutRule();
// 错误输出
@Rule
protected final static SystemErrRule logErr = new SystemErrRule();
/**
* 用于测试中,向控制台输出信息
* @author jerryjin
*/
public static class Log {
public void out(String msg) {
System.out.println("----------------------log.out begin-------------------------------");
System.out.println(msg);
System.out.println("-----------------------log.out end--------------------------------");
logOut.getLog();
}
public void err(String msg) {
System.out.println("----------------------log.err begin-------------------------------");
System.err.println(msg);
System.out.println("-----------------------log.err end--------------------------------");
logErr.getLog();
}
}
public String getMsg(String msg, Object... args) {
return String.format(msg, args);
}
}
引用日志工具基类
比如有些工具类的测试并不需要Spring
我就可以继承这个类。(Spring
加载还是点时间的,懒得等)
package com.jerry;
import com.jerry.tools.JUnitLogTool;
import junit.framework.TestCase;
/**
* {@linkplain JUnit} 单元测试基类
* <br>支持 JUnit 中日志输出
*/
public class BaseLogTest extends TestCase {
// JUnit日志输出工具类
protected static com.jerry.tools.JUnitLogTool.Log log = new JUnitLogTool.Log();
}