From the course: Programming Foundations: Test-Driven Development
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Assertions - Java Tutorial
From the course: Programming Foundations: Test-Driven Development
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.