JUnit4使用Assert类进行测试判断以及@Test注解属性

  • assertEquals----判断是否与某一特定值相等,相等则成功否则失败
  • assertTrue----判断某一值是否为true,是true则为成功,否则失败
  • assertFalse----判断某一值是否为false

@Test注解属性

expected----用于期望抛出某一类型异常,抛出异常测试成功,否则测试失败

timeout----用于测试超时(非功能性测试),在规定时间内完成测试则成功,否则失败


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

public class AssertTest {
	private String name;
	private boolean bool;
	@Before
	public void before() {
		name="anwar";
		bool=true;
	}
	@Test
	public void test() {
		assertEquals("测试失败,名字不是 anwar",name,"anwar");
	}
	@Test
	public void testBol() {
		assertTrue("测试失败,bool为 false",bool);
	}
	//预期抛出某种异常,抛出则成功,反之则失败
	@Test(timeout=1000l)
	public void exceptionTest() throws InterruptedException {
		System.out.println("将要抛出异常,测试可能会成功");
		Thread.sleep(2000);
		//throw new NullPointerException();
	}

}

 

你可能感兴趣的:(test)