JUnit Study Notes (2): hamcrest and testsuit

Source: Internet
Author: User
1. hamcrestHamcrest can effectively increase the JUnit testing capability and use some popular languages for testing. hamcrest is a testing framework. It provides a set of general matcher matching characters and flexibly uses the rules defined by these matching characters. programmers can express their testing ideas more accurately, specify the test conditions you want to set. For example, sometimes the scope of the defined test data is too accurate, usually a number of fixed values, this will lead to a very fragile test, because the next test data only needs to change slightly, the test may fail (for example, assertequals (x, 10
); You can only judge whether X is equal to 10. If X is not equal to 10, the test fails. Sometimes, the specified test data range is not accurate enough, in this case, some data that would have caused the test to fail may still pass the next test, which will reduce the price of the test. The emergence of hamcrest provides programmers with a set of rules and methods for writing test cases, so that they can more accurately express the Testing behavior that programmers expect.

Common hamcrest matchers:

  • Core

    • Anything-always matches. If you do not care about the objects under the test, what is useful?
    • Describedas-Add a custom failure decorator
    • Is-improved readability modifier-see "sugar"
  • Logic
    • Allof-if all the matching matches, short circuits (a difficult word, short circuit translation, no translation if it feels wrong) (like Java &&)
    • Anyof-if any matching matches, short circuits (like Java |)
    • Not-if the package does not match, and vice versa
  • Object
    • Similar to-equal test object use object. Equals Method
    • Hastostring-test the object. tostring method.
    • Instanceof, iscompatibletype-test Type
    • Notnullvalue, nullvalue-test null
    • Sameinstance-test object instance
  • Beans
    • Hasproperty-test JavaBeans attributes
  • Set
    • Array-test an array element test an array's elements against an array of matchers
    • Hasentry, haskey, hasvalue-test a map that contains an object, key, or value
    • Hasitem, hasitems-test a set that contains an element
    • Hasiteminarray-test whether an array contains an element
  • Number
    • Closeto-test that the floating point value is close to the given value
    • Greaterthan, greaterthanorequalto, lessthan, lessthanorequalto-test order
  • Text
    • Using toignoringcase-the test string is equal and case-insensitive.
    • Using toignoringwhitespace-test string ignore Blank
    • Containsstring, endswith, startswith-test string matching

1. Use assertthat in JUnit to assert and remember static import.

import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.*;import static org.junit.Assert.*;
2. Test instance:
@ Test public void testhamcrest () {// compare whether 50 is equal to 50 assertthat (50, limit to (50 )); // 50 is greater than 30 and less than 60 assertthat ("error", 50, allof (greaterthan (30), lessthan (60 ))); // complete the following assertthat ("error", "abc.txt", endswith (". TXT "));}
Note that if junit4.10 is used, the jar package of hamcrest must be moved to the jar package of JUnit. Otherwise, the combination condition allof will throw an exception. if testsuit has multiple test operation classes, it is inconvenient to run the test one by one. Therefore, you can use testsuit to "bundle" multiple test classes and test them together! For example, the following project has three test classes: Testa, testb, and testcalculate. We can create another testsuit class, package these classes, and run the test at the same time.
Package COM. fjnu. util; import Org. JUnit. runner. runwith; import Org. JUnit. runners. suite; import Org. JUnit. runners. suite. suiteclasses; // runwith indicates that this class is a suite class @ runwith (suite. class) // describes the test components included in this class @ suiteclasses ({Testa. class, testb. class, testcalculate. class}) public class testsuit {/** test principles: * 1. It is recommended to create a dedicated source folder ---> test to write the test class code * 2. The test class package should be consistent with the class to be tested * 3. Each test method in the test unit must be executed independently, no order, no mutual dependency */}

Run the testsuit class to test the selected test class.

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.