Adam Matan Adam Matan - 2 months ago 21
Java Question

assert(false) does not stop execution

In one of my JUnit test, I'm initializing an object:

MyObject myObject = new MyObject(220, 120, 0.05, true);


The constructor's signature is:

public MyObject(int minLength, int maxLength,
double onBitsRatio, boolean forceAtLeastOneBitOn)


Followed by:

assert(onBitsRatio >= 0.0 && onBitsRatio <= 1.0);
assert(maxLength>=minLength);
assert(false);


Oddly enough, the assertions don't stop the execution, as I expect them to.

Why is JUnit ignoring these assertions?

Answer

JUnit is not ignoring these assertions, because as you say, you are using Java's assert keyword. This is handled by the JVM, not JUnit.

The answer is almost certainly that your JVM assertions are turned off. In fact they are off unless you turn them on with -ea. These are ignored otherwise.