JAVA:JUNIT4 use of the detailed

Source: Internet
Author: User

Tag: timeout CEP for stat execution exception cannot on () runtime

I don't know much about JUnit's explanation, so let's look at it.

We are targeting the JUNIT4 version, the annotations are also available in the JUNIT4 version, the previous version has no annotation function. Annotation development is basically considered an excellent design, so when we write unit test cases, we try to understand and use annotations as much as possible.

@Test: The method used for this annotation is a test method, which can be recognized and executed by the automated runtime

Contains two parameters: Expected=xxxexception.class expected exception

Timeout=xxx setting the timeout period for the program to run

@Ignore: Ignored test methods

@Before: Run before each test method

@After: Run after each test method

@BeforeClass: Run before all tests are started

@AfterClass: Run after all tests have started

Attention:

(1) The difference between @Before and @beforeclass

@Before will be executed before each method, i.e. several @test will be executed several times.

@BeforeClass executes only before all tests are performed, only once. And the @beforeclass-modified method must be a public static method (common static).

@After and @afterclass are similar.

(2) These four annotations in each test class cannot be reused, and one method can only pick one of the four annotations.

Focus:

(3) If you do not want a method to execute: @Ignore () You can label a test method that does not participate in the test. This can also be achieved by removing the annotation @Test, but removing the annotation @Test will make it impossible to display the test method in the JUnit view of Eclipse.

Like what:

Package Gc;import static Org.junit.assert.*;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 JUnit4Test {@Beforepublic void before () {System.out.println ("@Before");}  @Testpublic void Test () {System.out.println ("@Test"); Assertequals (5 + 5, 10);} @Ignore @testpublic void Testignore () {System.out.println ("@Ignore"), @Test (timeout =) public void Testtimeout () {Sy  Stem.out.println ("@Test (timeout = 50)"); Assertequals (5 + 5, 10);} @Test (expected = arithmeticexception.class) public void testexpected () {System.out.println ("@Test (expected =  Exception.class) "); throw new ArithmeticException ();}  @Afterpublic void After () {System.out.println ("@After");  } @BeforeClass public static void Beforeclass () {System.out.println ("@BeforeClass");    };  @AfterClass public static void Afterclass () {System.out.println ("@AfterClass"); };};

The result is:

@BeforeClass @before@test (timeout = @After @before@test (expected = exception.class) @ After@before@test@after@afterclass

  

JAVA:JUNIT4 use of the detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.