JavaSE进阶
import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test;
public class TestJUnit2 { private static Object[] array; private static int total;
@BeforeClass public static void init(){ System.out.println("初始化数组"); array = new Object[5]; }
@Before public void before(){ System.out.println("调用之前total=" + total); }
@Test public void add(){ //往数组中存储一个元素 System.out.println("add"); array[total++] = "hello"; }
@After public void after(){ System.out.println("调用之前total=" + total); }
@AfterClass public static void destroy(){ array = null; System.out.println("销毁数组"); } } |
如何运行Junit测试方法
一个类中可以有多个@Test标记的方法,运行时如果只想运行其中一个@Test标记的方法,那么选择这个方法名,然后单独运行,否则整个类的所有标记了@Test的方法都会被执行。