JUnit 4 New assertion: A common approach to hamcrest

Source: Internet
Author: User

One, character-related matching characters

1, Equalto:

Assertthat (Testedvalue, Equalto (Expectedvalue));

Asserts that the measured testedvalue equals Expectedvalue,equalto can assert the equality between the values, between strings and objects, equivalent to the Equals method of object

2, Equaltoignoringcase:

Assertthat (testedstring, Equaltoignoringcase (expectedstring));

asserts that the string testedstring is measured is equal to expectedstring if the case is ignored

3, Equaltoignoringwhitespace:

Assertthat (testedstring, Equaltoignoringwhitespace (expectedstring);

asserts that the string testedstring is measured is equal to expectedstring if any of the trailing spaces are ignored

(Note: Spaces in a string cannot be ignored)

4, Containsstring:

Assertthat (testedstring, containsstring (subString));

Asserts that the measured string testedstring contains substrings substring

5, EndsWith:

Assertthat (testedstring, endsWith (suffix));

Assertion that the measured string testedstring ends with a substring suffix

6, StartsWith:

Assertthat (testedstring, startswith (prefix));

asserts that the string testedstring being measured starts with a substring prefix

Two, the general match character

1, Nullvalue ():

Assertthat (Object,nullvalue ());

Asserts that the value of the object being measured is null*/

2, Notnullvalue ():

Assertthat (Object,notnullvalue ());

Asserts that the value of the object being measured is not null*/

3, is:

Assertthat (Testedstring, is (Equalto (expectedvalue));

Asserts that the object being measured is equal to the following given matching expression

1 is a matching character shorthand application one:

Assertthat (Testedvalue, is (Expectedvalue));

The shorthand for IS (Equalto (x)), the assertion testedvalue equals Expectedvalue

2 is a matching character shorthand application of the second:

Assertthat (Testedobject, is (Cheddar.class));

Is (instanceof (someclass.class)) shorthand, assertion Testedobject as an instance of Cheddar

4, not:

Assertthat (testedstring, not (expectedstring));

Contrary to an is match, the assertion that the object being measured is not equal to the object given later

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.