Reputation: 136231
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?
Upvotes: 8
Views: 7892
Reputation: 11
From Java The Complete Reference:
" To enable assertion checking at run time, you must specify the -ea option. For example, to enable assertions for AssertDemo, execute it using this line:
java -ea AssertDemo
"
Upvotes: 1
Reputation: 24499
Are you sure you are using proper assert from JUnit?
If you are using normal java assert, these are turned off by default. You have to enable them explicitly.
Try to use this: org.junit.Assert.assertTrue(false)
As far as I know, there is no method in the JUnit Assert library that is called just assert()
Seeing as your asserts are in some object, and you want the test to fail, then you need to enable the assert. Take a look at this tutorial describing how you can enable assertions.
Upvotes: 6
Reputation: 11078
Change them to JUnit assertions, you are using plain Java assertions, that are disabled by default in the jvm:
assertTrue(onBitsRatio >= 0.0 && onBitsRatio <= 1.0);
assertTrue(maxLength>=minLength);
assertTrue(false);
If you really want to use java assertions, make sure you have -enableassertions
as a parameter in your run configuration.
As per your question tags you are using eclipse, so go to run as -> run configurations and add -enableassertions
or -ea
in the VM arguments tab.
Upvotes: 2
Reputation: 66886
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.
Upvotes: 25