From the course: Programming Foundations: Test-Driven Development

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Assertions

Assertions - Java Tutorial

From the course: Programming Foundations: Test-Driven Development

Start my 1-month free trial

Assertions

- Most programming languages have assert statement used for defensive programming to ensure that, if a certain condition is true, at a given point of time in the program execution. If the condition is not true, the program will throw an assertion error. In Java these assertions are by default disabled, and can be enabled by passing a parameter to the JVM at the time of executing the program. For example in this class Assertor, I'm invoking a method named assertSomething on an instance of Assertor. Here, I expect the value of x to be greater than five, so I can say assert followed by, the condition x greater than five. In this example, obviously, the condition will be true, and so the program will quietly pass without any errors. To demonstrate failure, I have now changed the condition. I am now asserting that x is less than five. As this assertion will be false, the program will throw an assertion error. This error will show up only if I have configured my JVM to enable assertions by…

Contents