//Person类
public class Person {
	public void run(){
		System.out.println("run");
	}
	public void eat(){
		System.out.println("eat");
	}
}


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

public class demo1 {
	
	private Person p;
	
	/*
	 * @BeforeClass
	 * 在测试类加载完成后第一个运行
	 * 这个方法必须为静态static
	 */
	@BeforeClass
	public static void beforeClass(){
		System.out.println("brforeClass");
	}
	
	
	
	
	/*
	 * 注解@Before
	 * 作用:在测试方法运行前运行,常用于测试方法前的初始化资源
	 */
	@Before
	public void before(){
		this.p = new Person();
		System.out.println("before");
	}
	
	/*
	 * 注解  @Test 
	 * 作用:测试方法,  Junit测试,方便简单
	 * 测试方法必须写在public类中
	 */
	@Test
	public void test1(){
		
		p.eat();
		
		
	}
	
	@Test
	public void test2(){
	
		p.run();	
		
	}
	
	/*
	 * 断言Assert
	 * 
	 */
	@Test
	public void Assert(){
		/*
		 * Assert.assertEquals(参数1,参数2)
		 * 判断参数2是否等于参数1
		 */
		junit.framework.Assert.assertEquals("ss", "ss");
		
		/*
		 * Assert.assertArrayEquals(参数1,参数2)
		 * 判断参数2数组是否等于参数1数组
		 */
		int array1[]={1,2,3};
		int array2[]={1,2,3};
		org.junit.Assert.assertArrayEquals(array1, array2);
	}
	
	
	
	
	/*
	 * 注解@After
	 * 作用在测试方法运行后运行,常用于销毁资源
	 */
	@org.junit.After
	public void After(){
		this.p=null;
		System.out.println("after");
	}
	
	/*
	 * @AfterClass
	 * 在测试类最后运行的方法
	 * 这个方法必须为静态static
	 */
	@org.junit.AfterClass
	public  static void AfterClass(){
		System.out.println("AfterClass");
	}
	
	
	
	
}



Junit基本用法

之前不会Junit测试代码都写了个main函数,现在学了Junit做一个测试类,不知道多方便,

这些知识基本用法