Various assertions of JUnit

Source: Internet
Author: User

From: http://dongbiying.iteye.com/blog/1002188

JUnit provides us with helper functions that help us determine whether the tested method works as expected. Usually, these helper functions are called assertions. Next we will introduce various assertions of JUnit.

1. assertEquals

Function prototype 1: assertEquals ([String message], expected, actual)

Parameter description:

Message is an optional message. If it is provided, the message will be reported when an error occurs.

Expected is the expected value, which is usually the content specified by the user.

Actual is the actual value returned by the tested code.

Example: assertEquals ("equals", "1", "1 ");

Function prototype 2: assertEquals ([String message], expected, actual, tolerance)

Parameter description:

Message is an optional message. If it is provided, the message will be reported when an error occurs.

Expected is the expected value, which is usually the content specified by the user.

Actual is the actual value returned by the tested code.

Tolerance is an error parameter. The two floating point numbers involved in the comparison are considered to be

Equal.

Example: assertEquals ("yes", 5.8, 11.0/2.0, 0.5 );

2. assertTrue

Function prototype: assertTrue ([String message], Boolean condition)

Parameter description:

Message is an optional message. If it is provided, the message will be reported when an error occurs.

Condition is the Boolean value to be verified.

This asserted is used to verify whether the given Boolean value is true. If the result is false, the verification fails. Of course, there are more test conditions for false verification:

Function prototype: assertFalse ([String message], Boolean condition)

This assertion is used to verify whether the given Boolean value is false. If the result is true, the verification fails.

Example: assertTrue ("true", 1 = 1 );

AssertFalse ("false", 2 = 1 );

3. assertNull

Function prototype: assertNull ([String message], Object object)

Parameter description:

Message is an optional message. If it is provided, the message will be reported when an error occurs.

Object is the object to be verified.

This assertion is used to verify whether the given object is null. If it is not null, verification fails. Correspondingly, there are still assertions that can verify non-null:

Function prototype: assertNotNull ([String message], Object object)

This assertion is used to verify whether the given object is non-null. If it is null, the verification fails.

Example: assertNull ("null", null );

AssertNotNull ("not null", new String ());

4. assertSame

Function prototype: assertSame ([String message], expected, actual)

Parameter description:

Message is an optional message. If it is provided, the message will be reported when an error occurs.

Expected is the expected value.

Actual is the actual value returned by the tested code.

This assertion is used to verify whether the expected and actual parameters reference the same object. If not, the verification fails. Correspondingly, there are also assertions for verifying that the object is not the same:

Function prototype: assertNotSame ([String message], expected, actual)

This assertion is used to verify whether the expected and actual parameters reference different objects. If the referenced objects are the same, the verification fails.

Example: assertSame ("same", 2, 4-2 );

AssertNotSame ("not same", 2, 4-3 );

5. Fail

Function prototype: Fail ([String message])

Parameter description:

Message is an optional message. If it is provided, the message will be reported when an error occurs.

This assertion causes the test to fail immediately and is usually used on the branch (for example, exception) that cannot be reached by the test ).

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.