From: SourceForge.net <no...@so...> - 2008-03-12 22:03:03
|
Feature Requests item #1913113, was opened at 2008-03-13 00:03 Message generated for change (Tracker Item Submitted) made by Item Submitter You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=365278&aid=1913113&group_id=15278 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Alex (toalexsmail) Assigned to: Nobody/Anonymous (nobody) Summary: Failures, Assertion and AssertionError Initial Comment: In JUnit 3.8 there was difference between assertion error (assertTrue(flag) fails, for example) and exception (unexpected exception thrown). It was indicated as F (failures) and E (exception). Why this was removed from JUnit 4? Moreover, if asserts of JDK 1.4 was used (assert obj!=null;) both in the tested code and in the tests than, if assertion is enabled, AssertionError was thrown that caused to exception. In JUnit 4 it will cause to failure because AssertionFailedError now extends AssertionError so from JUnit point of view there is no difference. It is true, that in JUnit 4 there is no difference between failures and exception, so it shouldn't be a problem. But, as I see it is. Consider for example following example. public class Calculator { ... public double sqrt(double a){ //here should be check that a>=0 that is missing return doSqrt(a); } private double doSqrt(double a){ //a should be valid at this point assert "a should be not negative!" : a>=0; ... } } public class CalculatorTest { // the logger private static final Log log = LogFactory.getLog(CalculatorTest.class); private Calculator calc; @BeforeClass public static void oneTimeSetup(){ // do your one time set-up here! log.debug("Going to check whether asserts are enabled"); boolean assertsEnabled = false; assert (assertsEnabled = true); // Intentional side effect!!! if (!assertsEnabled) { throw new AssertionError("Asserts must be enabled!!!"); } } @Before public void setUp() throws Exception { calc = new Calculator(...); } public static void main(String[] args) throws Exception { Request r = Request.aClass(clazz); JUnitCore runner = new JUnitCore(); RunListener listener= new TextListener(); runner.addListener(listener); runner.run(r); } @Test public void testNegative() throws Exception { try { calc.sqrt(-1); fail("IllegalArgumenException is expecting"); catch(IllegalArgumenException e){ assert true : "it' ok"; } } } assert are used inside the code in order to validate what programmer is think is true in some point of the program. They are often used to check preconditions of the private methods. In the above example, programmer that writed doSqrt() method states that precondition of this method is that he receives valid parameter. So, this JDK 1.4 assertion fails has nothing with JUnit 4 assertXXX() fails and it should be considered as exception, as in case that unexpected exception is thrown. IllegalArgumenException should be thrown in public sqrt() method. This is not happened because programmer forgot to do this check. It is the bug. Instead AssertionError is thrown by the code itself, because programmer that writed doSqrt() method clearly states that his precondition is that argument is valid. We should be fails before. AssertionError shouldn't happen. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=365278&aid=1913113&group_id=15278 |