System Rules 更好的測試


1:編寫測試事例時候,我們要從控制台拿到數據與斷言進行對比,通常要編寫jdk 標准輸出的屏蔽控制器.文章標題的包,能夠更好的為我們進行工作.

package demo2;

import static org.junit.Assert.assertEquals;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;

public class demo1 {
	
	@Rule // 過去寫法
	private final StandardOutputStreamLog log=new StandardOutputStreamLog();
	
	//正確輸出
	@Rule 
	private final SystemOutRule sRule=new SystemOutRule();
	
	//錯誤輸出
	@Rule
	private final SystemErrRule errrule=new SystemErrRule();
	
	@Test
	public void miantest() {
		sysou();
		assertEquals(2, sRule.getLog());
	}

	private void sysou() {
		System.out.println(2);
	}

}

其實使用事例介紹

Clear Properties

public class MyTest {
	@Rule
	public final ClearSystemProperties myPropertyIsCleared
	 = new ClearSystemProperties("MyProperty");

	@Test
	public void overrideProperty() {
		assertNull(System.getProperty("MyProperty"));
	}
}

Provide Properties

public class MyTest {
	@Rule
	public final ProvideSystemProperty myPropertyHasMyValue
	 = new ProvideSystemProperty("MyProperty", "MyValue");

	@Rule
	public final ProvideSystemProperty otherPropertyIsMissing
	 = new ProvideSystemProperty("OtherProperty", null);

	@Test
	public void overrideProperty() {
		assertEquals("MyValue", System.getProperty("MyProperty"));
		assertNull(System.getProperty("OtherProperty"));
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM