What does this java statement mean ?

UI Components for JSF
Post Reply
robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

11 Dec 2010, 13:16

Assert is a testing mechanism of JUnit. It Checks if the statement it's getting called with is true or false. When true, it continues execution, when false, it throws an exception. This is basically the clean way of testing.
Instead of something like this:

Code: Select all

if (a == null || b == null || a != b) {
    throw new Exception(...);
} else {
    // do Something
}
You can write the code like this:

Code: Select all

Assert.assertNotNull("a is not allowed to be null", a);
Assert.assertNotNull("b is not allowed to be null", b);
Assert.assertEquals("a has to be equal to b", a, b);
// do Something
You can check out the documentation here:
http://www.junit.org/apidocs/org/junit/Assert.html

averri
Posts: 136
Joined: 07 Sep 2009, 15:51
Location: Brazil

14 Dec 2010, 15:55

I would like to complement the information.

An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. Each assertion contains a boolean expression that must be true when the assertion executes. If it is not true, the system will throw an error.

By default, assertions are disabled at runtime. To enable assertions, use the -enableassertions, or -ea, switch when lauching a java application.

Although the JUnit has a Assert class for testing purposes, the 'assert' (mentioned in the question) is a keyword of Java.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Baidu [Spider] and 30 guests