JUnit uses the first bullet

Source: Internet
Author: User

Knowledge Point-assertion

Assertions are the core implementation of writing test cases, that is, what the expected value is, and how much is the result of the test to determine whether the test passed.

1. Assert the core approach

Assertarrayequals (expecteds, actuals) See if two arrays are equal.
Assertequals (expected, actual) See if two objects are equal. The Equals () method, similar to the use of string comparisons
Assertnotequals (first, second) See if two objects are not equal.
Assertnull (object) View whether the object is empty.
Assertnotnull (object) View whether the object is not empty.
Assertsame (expected, actual) See if the references to two objects are equal. Similar to using "= =" to compare two objects
Assertnotsame (Unexpected, actual) See if the references to two objects are not equal. Similar to using "! =" to compare two objects
Asserttrue (condition) View whether the run result is true.
Assertfalse (condition) See if the run result is false.
Assertthat (Actual, Matcher) See if the actual value meets the specified criteria
Fail () Let the test fail

2. Example

  1. Package test;
  2. Import static org.hamcrest.corematchers.*;
  3. Import static org.junit.assert.*;
  4. Import Java.util.Arrays;
  5. Import Org.hamcrest.core.CombinableMatcher;
  6. Import Org.junit.Test;
  7. Public class Asserttests {
  8. @Test
  9. public void Testassertarrayequals () {
  10. byte[] Expected = "trial". GetBytes ();
  11. byte[] actual = "trial". GetBytes ();
  12. Org.junit.Assert.assertArrayEquals ("failure-byte arrays not Same", expected, actual);
  13. }
  14. @Test
  15. public void Testassertequals () {
  16. Org.junit.Assert.assertEquals ("failure-strings not Same", 5l, 5l);
  17. }
  18. @Test
  19. public void Testassertfalse () {
  20. Org.junit.Assert.assertFalse ("failure-should be false", false);
  21. }
  22. @Test
  23. public void Testassertnotnull () {
  24. Org.junit.Assert.assertNotNull ("should not is null", new Object ());
  25. }
  26. @Test
  27. public void Testassertnotsame () {
  28. Org.junit.Assert.assertNotSame ("should not being same object", new Object (), new Object ());
  29. }
  30. @Test
  31. public void Testassertnull () {
  32. Org.junit.Assert.assertNull ("should be null", null);
  33. }
  34. @Test
  35. public void Testassertsame () {
  36. Integer anumber = integer.valueof (768);
  37. Org.junit.Assert.assertSame ("should be Same", anumber, Anumber);
  38. }
  39. //JUnit matchers assertthat
  40. @Test
  41. public void Testassertthatbothcontainsstring () {
  42. Org.junit.Assert.assertThat ("albumen", Both (containsstring ("a")). and (Containsstring ("B"));
  43. }
  44. @Test
  45. public void Testassertthathasitemscontainsstring () {
  46. Org.junit.Assert.assertThat ("One", "Arrays.aslist", " three"), Hasitems ("One", "three");
  47. }
  48. @Test
  49. public void Testassertthateveryitemcontainsstring () {
  50. Org.junit.Assert.assertThat (Arrays.aslist (new string[] { "fun", "ban", "NET"}), Everyitem (  Containsstring ("n")));
  51. }
  52. //Core hamcrest matchers with Assertthat
  53. @Test
  54. public void Testassertthathamcrestcorematchers () {
  55. Assertthat ("Good", AllOf (Equalto ("good"), StartsWith ("good"));
  56. Assertthat ("Good", Not (AllOf (Equalto ("bad"), Equalto ("good")));
  57. Assertthat ("Good", AnyOf (Equalto ("bad"), Equalto ("good"));
  58. Assertthat (7, not (combinablematcher.<integer> either (Equalto (3)). or (Equalto (4)));
  59. Assertthat (new Object (), not (Sameinstance (new Object ())));
  60. }
  61. }

Knowledge points--annotations

1. Description

@Before Initialize method
@After Freeing resources
@Test Test method, where you can test for expected exceptions and time-outs
@Ignore Ignored test methods
@BeforeClass For all tests, execute only once and must be static void
@AfterClass For all tests, execute only once and must be static void
@RunWith Specifies that a test class uses a runner
@Parameters To specify a test data collection for a test class
@Rule Allows flexibility to add or redefine the behavior of each test method in a test class
@FixMethodOrder Specify the order in which test methods are executed

2. Execution order

A Test class unit test is executed in the following order:

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass

The order in which each test method is called is:

@Before –> @Test –> @After

3. Example

  1. Package test;
  2. Import static org.junit.assert.*;
  3. Import org.junit.*;
  4. Public class Jdemotest {
  5. @BeforeClass
  6. public static void Setupbeforeclass () throws Exception {
  7. System.out.println ("in beforeclass================");
  8. }
  9. @AfterClass
  10. public static void Teardownafterclass () throws Exception {
  11. System.out.println ("in afterclass=================");
  12. }
  13. @Before
  14. public Void before () {
  15. System.out.println ("in Before");
  16. }
  17. @After
  18. public Void after () {
  19. System.out.println ("in after");
  20. }
  21. @Test (timeout = 10000)
  22. public void Testadd () {
  23. Jdemo a = new Jdemo ();
  24. Assertequals (6, A.add (3, 3));
  25. System.out.println ("in Test----Add");
  26. }
  27. @Test
  28. public void Testdivision () {
  29. Jdemo a = new Jdemo ();
  30. Assertequals (3, A.division (6, 2));
  31. System.out.println ("in Test----Division");
  32. }
  33. @Ignore
  34. @Test
  35. public void Test_ignore () {
  36. Jdemo a = new Jdemo ();
  37. Assertequals (6, A.add (1, 5));
  38. System.out.println ("in Test_ignore");
  39. }
  40. @Test
  41. public void Teest_fail () {
  42. Fail ();
  43. }
  44. }
  45. Class Jdemo extends Thread {
  46. int result;
  47. public int Add (int A, int b) {
  48. try {
  49. Sleep (1000);
  50. result = a + B;
  51. } catch (Interruptedexception e) {
  52. }
  53. return result;
  54. }
  55. Public int Division (int A, int b) {
  56. return result = A/b;
  57. }
  58. }

Execution Result:

    1. In beforeclass================
    2. In Before
    3. In Test----ADD
    4. In after
    5. In Before
    6. In Test----Division
    7. In after
    8. In afterclass=================

The upper left red box in the figure represents the JUnit run result, 5 successes (one ignore), 1 errors, and 1 failures. (Note that errors and failures are not the same, the error indicates that the code has an error, and failure indicates that the test method test failed)

The lower left red box indicates the running state of each test method, and you can see that the respective icons for success, error, failure, and failure are different, and you can see the run time.

The right part is the exception stack, which allows you to view exception information.

JUnit uses first bullet

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.