Java Unit Test Comment execution order

Source: Internet
Author: User

JUNIT4 identifies the test method by annotating it. The main annotations currently supported are:

    • @BeforeClassThe global is executed only once, and is the first one to run
    • @BeforeRun before the test method runs
    • @TestTest method
    • @AfterAllows after the test method has been run
    • @AfterClassThe global only executes once and is the last one to run
    • @IgnoreIgnore this method

Here's an example:

  1. Import Org.junit.After;
  2. Import Org.junit.AfterClass;
  3. Import Org.junit.Assert;
  4. Import Org.junit.Before;
  5. Import Org.junit.BeforeClass;
  6. Import Org.junit.Ignore;
  7. Import Org.junit.Test;
  8. Public class Junit4testcase {
  9. @BeforeClass
  10. public static void Setupbeforeclass () {
  11. System.out.println ("Set Up Before class");
  12. }
  13. @Before
  14. public void SetUp () throws Exception {
  15. System.out.println ("Set up");
  16. }
  17. @Test
  18. public void Testmathpow () {
  19. System.out.println ("Test Math.pow");
  20. Assert.assertequals (4.0, Math.pow (2.0, 2.0), 0.0);
  21. }
  22. @Test
  23. public void Testmathmin () {
  24. System.out.println ("Test math.min");
  25. Assert.assertequals (2.0, Math.min (2.0, 4.0), 0.0);
  26. }
  27. //Expect this method to throw a NullPointerException exception
  28. @Test (expected = nullpointerexception. Class)
  29. public void TestException () {
  30. System.out.println ("Test exception");
  31. Object obj = null;
  32. Obj.tostring ();
  33. }
  34. //Ignore this test method
  35. @Ignore
  36. @Test
  37. public void Testmathmax () {
  38. Assert.fail ("not implemented");
  39. }
  40. //Use "assumptions" to ignore test methods
  41. @Test
  42. public void Testassume () {
  43. System.out.println ("Test assume");
  44. //When the assumption fails, it stops running, but this does not mean that the test method failed.
  45. Assume.assumetrue (false);
  46. Assert.fail ("not implemented");
  47. }
  48. @After
  49. public void TearDown () throws Exception {
  50. System.out.println ("Tear down");
  51. }
  52. @AfterClass
  53. public static void Teardownafterclass () {
  54. System.out.println ("Tear down after Class");
  55. }
  56. }

JUNIT3 's package is junit.framework , and JUNIT4 is org.junit .
After you execute this use case, the console outputs

Wrote set up before class
Set up
Test Math.pow
Tear down
Set up
Test Math.min
Tear down
Set up
Test exception
Tear down
Set up
Test assume
Tear down
Tear down after class

As you can see, the order of execution is-- @BeforeClass @Before @Test @After @Before @Test @After @AfterClass ----------. @Ignorewill be ignored.

Java Unit Test Comment execution order

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.