JUnit 输出日志到控制台

添加依赖到 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();
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
JUnit 4 是JUnit测试框架的最新版本,它与JUnit 3有很多不同之处。以下是将JUnit 3升级到JUnit 4的步骤: 1. 导入JUnit 4库 - 在项目的构建路径中,将JUnit 4库导入到项目中。 2. 更改测试注释 - 在JUnit 3中,测试方法使用“@Test”注释来标记。在JUnit 4中,测试方法应使用“@Test”注释来标记。 3. 更改断言语句 - 在JUnit 3中,断言语句使用“assertTrue”或“assertFalse”等方法来检查条件。在JUnit 4中,可以使用更多的断言方法,例如“assertEquals”和“assertNotNull”。 4. 更改测试套件 - 在JUnit 3中,测试套件作为一个类来实现,并且必须继承TestCase类。在JUnit 4中,测试套件可以使用“@RunWith”注释来标记,并且可以使用“@SuiteClasses”注释来指定包含的测试类。 5. 使用注释取代传统的测试方法 - 在JUnit 4中,可以使用注释来指定测试方法的预备条件,例如@Before和@After。这些注释可以用于所有测试方法,而不是只能用于继承自TestCase的测试方法。 6. 使用参数化测试 - 在JUnit 4中,可以使用“@Parameterized”注释来标记测试方法,从而使测试方法可以接受参数。这样可以更轻松地测试不同的输入和输出。 7. 使用JUnit 4规则 - JUnit 4包含许多规则(例如TemporaryFolder)来帮助测试。可以使用“@Rule”注释来标记规则。 通过这些步骤,就可以将JUnit 3升级到JUnit 4,并且可以使用JUnit 4的新功能来改进测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑虾

多情黯叹痴情癫。情癫苦笑多情难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值