Loading ...
Sorry, an error occurred while loading the content.

Catching certain Exception types for all test cases

Expand Messages
  • jogrimst
    Hello, I am currently developing WebDriver test cases, which all might produce exceptions named WebDriverException. Is it some how possible to catch (and
    Message 1 of 2 , Jan 1, 2011
    • 0 Attachment
      Hello,

      I am currently developing WebDriver test cases, which all might produce exceptions named WebDriverException.

      Is it some how possible to catch (and handle) certain Exception types for the entire test suite collection in a webapp?

      I.e., if I have multiple packages under src/test/java/ which each contain multiple Java classes (JUnit), is there any way to detect when certain Exceptions are thrown and then handle them in one single place?

      Example:

      Now I am using the following code for Exception handling:

      @Test
      public void testSomething() throws NoSuchElementException {
      try {
      // Code...
      } catch (NoSuchElementException e) {
      // Handle exception
      throw e;
      }
      }

      Now, I have multiple classes using this try-catch teqhnique. So, is there a way to avoid this, i.e. can JUnit somehow detect when a certain type of Exception occurs, making it possible for me to perform exception handling before continuing as normal?

      Regards
      jogrimst
    • David Saff
      jogrimst, It depends. With Rules in JUnit 4, you can hook up a Rule that will have a chance to handle any exceptions thrown by all test methods in a class,
      Message 2 of 2 , Jan 3, 2011
      • 0 Attachment
        jogrimst,

        It depends. With Rules in JUnit 4, you can hook up a Rule that will
        have a chance to handle any exceptions thrown by all test methods in a
        class, and you can add that Rule to each class with a single line.

        However, it looks like you might be hoping to restart the test method
        after the line that throws an exception. If you're looking for that,
        I'm not aware of any Java package that has that capability.

        Good luck,

        David Saff

        On Sat, Jan 1, 2011 at 5:03 PM, jogrimst <jo.grimstad@...> wrote:
        > Hello,
        >
        > I am currently developing WebDriver test cases, which all might produce exceptions named WebDriverException.
        >
        > Is it some how possible to catch (and handle) certain Exception types for the entire test suite collection in a webapp?
        >
        > I.e., if I have multiple packages under src/test/java/ which each contain multiple Java classes (JUnit), is there any way to detect when certain Exceptions are thrown and then handle them in one single place?
        >
        > Example:
        >
        > Now I am using the following code for Exception handling:
        >
        >    @Test
        >    public void testSomething() throws NoSuchElementException {
        >        try {
        >            // Code...
        >        } catch (NoSuchElementException e) {
        >            // Handle exception
        >            throw e;
        >        }
        >    }
        >
        > Now, I have multiple classes using this try-catch teqhnique. So, is there a way to avoid this, i.e. can JUnit somehow detect when a certain type of Exception occurs, making it possible for me to perform exception handling before continuing as normal?
        >
        > Regards
        > jogrimst
        >
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.