Exception Handling for JUnit 4.x and newer

JUnit 4.x and newer versions allow the definition of methods that throw exceptions. Throwing such exceptions is validated by JUnit. Silk Performer imports JUnit methods that throw exceptions including the validation of the thrown message.
    JavaRegisterException(500,"java.lang.IllegalArgumentException", JAVA_OPTION_MATCH_SUBSTRING);
    JUnit4CallFunc(ghTestObj, "doFoo", "doFoo", sBuffer);
    ErrorRemove(FACILITY_NATIVE_JAVA, 500);
    if (StrSearch(sBuffer, "java.lang.IllegalArgumentException", STR_SEARCH_FIRST) = 0) then 
          RepMessage("method did not throw: java.lang.IllegalArgumentException", SEVERITY_ERROR);

The sample above tests a JUnit method that is expected to throw a java.lang.IllegalArgumentException. The function JavaRegisterException assigns the BDL error code 500 to the exception. The function ErrorAdd sets the error severity to informational because the error is expected. After that, the JUnit method doFoo is invoked, where the parameter sBuffer retrieves the error message. The ErrorRemove function sets the error handling back to its original state. It also verifies that the expected exception occurred, and throws a BDL error if this is not the case.

Note: When modifying the BDL script manually, make sure that you do not map a Java exception to different BDL error numbers, or vice versa.