Various assertions of Java JUnit

Source: Internet
Author: User
Tags function prototype object object

JUnit provides us with some auxiliary functions that help us determine whether the method being tested works as expected, often referred to as assertions. Let's take a look at the various assertions of JUnit.

1, Assertequals

function prototype 1:assertequals ([String message],expected,actual)

parameter Description:

The message is optional and, if provided, will be reported when an error occurs.

expected are expected and are typically user-specified content.

actual is the actual value returned by the code being tested.

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

function prototype 2:assertequals ([String message],expected,actual,tolerance)

parameter Description:

The message is optional and, if provided, will be reported when an error occurs.

expected are expected and are typically user-specified content.

actual is the actual value returned by the code being tested.

tolerance is the error parameter, and the two floating-point numbers participating in the comparison are considered

equal.

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

2, Asserttrue

function Prototype: asserttrue ([String message],boolean condition)

parameter Description:

The message is optional and, if provided, will be reported when an error occurs.

the condition is a Boolean value to be validated.

the assertion is used to verify that the given Boolean value is true and the validation fails if the result is false. Of course, there are more test conditions that are verified as false:

function Prototype: Assertfalse ([String message],boolean condition)

the assertion is used to verify that a given Boolean value is false, and if the result is true, the validation fails.

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

Assertfalse ("false", 2==1);

3, Assertnull

function Prototype: assertnull ([String message],object Object)

parameter Description:

The message is optional and, if provided, will be reported when an error occurs.

object is the one you want to validate.

the assertion is used to verify that the given object is null, and if it is not NULL, the validation fails. Accordingly, there are assertions that can verify non-null:

function Prototype: assertnotnull ([String message],object Object)

the assertion is used to verify that the given object is non-null, and if NULL, the validation fails.

Example: Assertnull ("null", NULL);

assertnotnull ("Not NULL", New String ());

4, Assertsame

function Prototype: assertsame ([String message], expected,actual)

parameter Description:

The message is optional and, if provided, will be reported when an error occurs.

expected is expected.

actual is the actual value returned by the code being tested.

the assertion is used to verify that the expected parameter and the actual parameter refer to the same object, and if not, the validation fails. Accordingly, there is also an assertion that verifies that it is not the same object:

function Prototype: assertnotsame ([String message], expected,actual)

the assertion is used to verify that the expected parameter and the actual parameter refer to a different object, and if the referenced object is the same, the validation fails.

Example: Assertsame ("Same", 2,4-2);

assertnotsame ("Not Same", 2,4-3);

5. Fail

function Prototype: Fail ([String message])

parameter Description:

The message is optional and, if provided, will be reported when an error occurs.

This assertion causes the test to fail immediately, usually on a branch that the test cannot reach (such as an exception).

Various assertions of Java JUnit

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.