JUnit – Basic annotation examples
2017-07-09 00:00
429 查看
摘要: 转载自http://www.mkyong.com/unittest/junit-4-tutorial-1-basic-usage/
Here’re some basic JUnit annotations you should understand:
1.@BeforeClass – Run once before any of the test methods in the class, public static void
2.@AfterClass – Run once after all the tests in the class have been run, public static void
3.@Before – Run before @Test, public void
4.@After – Run after @Test,** public void**
5.@Test – This is the test method to run,** public void**
输出结果:
Here’re some basic JUnit annotations you should understand:
1.@BeforeClass – Run once before any of the test methods in the class, public static void
2.@AfterClass – Run once after all the tests in the class have been run, public static void
3.@Before – Run before @Test, public void
4.@After – Run after @Test,** public void**
5.@Test – This is the test method to run,** public void**
P.S Tested with JUnit 4.12
BasicAnnotationTest.javapackage com.mkyong; import org.junit.*; public class BasicAnnotationTest { // Run once, e.g. Database connection, connection pool @BeforeClass public static void runOnceBeforeClass() { System.out.println("@BeforeClass - runOnceBeforeClass"); } // Run once, e.g close connection, cleanup @AfterClass public static void runOnceAfterClass() { System.out.println("@AfterClass - runOnceAfterClass"); } // Should rename to @BeforeTestMethod // e.g. Creating an similar object and share for all @Test @Before public void runBeforeTestMethod() { System.out.println("@Before - runBeforeTestMethod"); } // Should rename to @AfterTestMethod @After public void runAfterTestMethod() { System.out.println("@After - runAfterTestMethod"); } @Test public void test_method_1() { System.out.println("@Test - test_method_1"); } @Test public void test_method_2() { System.out.println("@Test - test_method_2"); } }
输出结果:
@BeforeClass - runOnceBeforeClass @Before - runBeforeTestMethod @Test - test_method_1 @After - runAfterTestMethod @Before - runBeforeTestMethod @Test - test_method_2 @After - runAfterTestMethod @AfterClass - runOnceAfterClass
相关文章推荐
- JUnit basic annotation
- JUnit basic annotation
- JUnit——Annotation
- JUnit扩展:引入新注解Annotation
- Java Annotation Examples
- 15 Basic ‘ls’ Command Examples in Linux
- junit 单元测试 @Test报错:@Test is not an annotation type
- Github Basic Examples
- JUnit中使用Annotation获得实例
- JUnit 4 使用 Java 5 中的注解(annotation)
- 第一篇:Configuration-basic 之Spring对注解的处理【AnnotationConfigApplicationContext 】深度剖析
- Java之--------JUnit软件测试技术(工具)Annotation1
- 10 basic examples of linux netstat command
- Python Basic Examples
- Basic chkconfig Command Examples in Linux
- 使用Junit出现Type mismatch: cannot convert from Test to Annotation
- java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码
- 使用Junit出现Type mismatch: cannot convert from Test to Annotation
- 使用 Java Annotation 定制 Ant Junit Report
- basic examples of linux netstat command