Adam Matan Adam Matan - 1 year ago 147
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);

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

Why is JUnit ignoring these assertions?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download