Interview Questions

Should You Run JUnit Tests with Java Assertion Disabled?

JUnit Questions and Answers


(Continued from previous question...)

Should You Run JUnit Tests with Java Assertion Disabled?

If your test class is well designed and covering all cases where the target class could break, you should run your test with Java assertion disabled by using the JVM "-da" option.

If you are using the test class, DayOfWeekTest.java, presented in the previous question, here is the error report when executed with the "-da" option, disabling Java assertion:

java -da -cp .;\local\junit4.4\junit-4.4.jar
   org.junit.runner.JUnitCore DayOfWeekTest

JUnit version 4.4
...E
Time: 0.015
There was 1 failure:
1) test3(DayOfWeekTest)
java.lang.AssertionError: Expecting Friday
 at org.junit.Assert.fail(Assert.java:74)
 at org.junit.Assert.assertTrue(Assert.java:37)
 at DayOfWeekTest.test3(DayOfWeekTest.java:20)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 ...
FAILURES!!!
Tests run: 3,  Failures: 1

test3() failed with the JUnit assertTrue() call, and caught the code problem with negative values.

(Continued on next question...)

Other Interview Questions