JUnit 中的方法执行过程,即哪一个方法首先被调用,哪一个方法在一个方法之后调用。以下为 JUnit 测试方法的 API,并且会用例子来说明。
在目录 C:\ > JUNIT_WORKSPACE 创建一个 java 类文件命名为 JunitAnnotation.java 来测试注释程序。
import org.junit.After; |
import org.junit.AfterClass; |
import org.junit.Before; |
import org.junit.BeforeClass; |
import org.junit.Ignore; |
import org.junit.Test; |
public class ExecutionProcedureJunit { |
//execute only once, in the starting |
@BeforeClass |
public static void beforeClass() { |
System.out.println("in before class"); |
} |
//execute only once, in the end |
@AfterClass |
public static void afterClass() { |
System.out.println("in after class"); |
} |
//execute for each test, before executing test |
@Before |
public void before() { |
System.out.println("in before"); |
} |
//execute for each test, after executing test |
@After |
public void after() { |
System.out.println("in after"); |
} |
//test case 1 |
@Test |
public void testCase1() { |
System.out.println("in test case 1"); |
} |
//test case 2 |
@Test |
public void testCase2() { |
System.out.println("in test case 2"); |
} |
} |
import org.junit.runner.JUnitCore; |
import org.junit.runner.Result; |
import org.junit.runner.notification.Failure; |
public class TestRunner { |
public static void main(String[] args) { |
Result result = JUnitCore.runClasses(ExecutionProcedureJunit.class); |
for (Failure failure : result.getFailures()) { |
System.out.println(failure.toString()); |
} |
System.out.println(result.wasSuccessful()); |
} |
} |
C:\JUNIT_WORKSPACE>javac ExecutionProcedureJunit.java TestRunner.java |
C:\JUNIT_WORKSPACE>java TestRunner |
in before class |
in before |
in test case 1 |
in after |
in before |
in test case 2 |
in after |
in after class |