1 2 3 Previous Next 41 Replies Latest reply: Jul 30, 2007 1:35 PM by 796440 RSS

    junit - test for exception problem

    807605
      I'm trying to test if an exception is being thrown (when one is expected)
      I get this error (when I shouldn't):
      java.lang.AssertionError: Expected exception: exception.OutOfRangeException

      The method being tested:
      public void setScore ( int score ) throws exception.OutOfRangeException
      {
        if ( ( score < 0 ) || ( score > 100 ) ) {
            throw new exception.OutOfRangeException( "Score invalid: " + score );
        }
        else {
            this.score = score 
        }
      }
      The test method:
      @Test(expected=exception.OutOfRangeException.class)
      public void testScoreOutOfRangeException() {
          try {
              record.setScore (-1);
          }
          catch ( OutOfRangeException e ) {
               // do nothing because we expected it
          }
      }
      What I am doing wrong?
        • 1. Re: junit - test for exception problem
          807605
          I'm trying to test if an exception is being thrown
          (when one is expected)
          I get this error (when I shouldn't):
          java.lang.AssertionError: Expected exception:
          exception.OutOfRangeException

          The method being tested:
          public void setScore ( int score ) throws
          exception.OutOfRangeException
          {
          if ( ( score < 0 ) || ( score > 100 ) ) {
          throw new exception.OutOfRangeException( "Score
          invalid: " + score );
          }
          else {
          this.score = score 
          }
          The test method:
          @Test(expected=exception.OutOfRangeException.cla
          ss)
          public void testScoreOutOfRangeException() {
          try {
          record.setScore (-1);
          }
          catch ( OutOfRangeException e ) {
          // do nothing because we expected it
          
          }
          What I am doing wrong?
          Try this:
          catch ( exception.OutOfRangeException e ) {
          • 2. Re: junit - test for exception problem
            jschellSomeoneStoleMyAlias
            Try this:

            catch ( exception.OutOfRangeException e ) {
            Huh?

            Does that even compile?
            • 3. Re: junit - test for exception problem
              jschellSomeoneStoleMyAlias
              What I am doing wrong?
              You are catching the exception.

              junit expects to see the exception itself. You are catching it so it doesn't see it.
              • 4. Re: junit - test for exception problem
                807605
                Try this:

                catch ( exception.OutOfRangeException e ) {
                Huh?

                Does that even compile?
                public class exception {
                
                     static class OutOfRangeException extends Exception {}
                     
                     public static void main(String[] args)  {
                          try {
                               throwIt();
                          } catch (exception.OutOfRangeException e) {
                               e.printStackTrace();
                          }
                     }
                
                     private static void throwIt() throws OutOfRangeException{
                          
                     }
                     
                }
                Not that I'm advocating such code, of course :-)
                • 5. Re: junit - test for exception problem
                  807605
                  Yes, it does compile.
                  You are catching the exception.
                  junit expects to see the exception itself. You are catching it so it doesn't see it.
                  I understand what you are saying. I originally didn't type in the try/catch in the test method. But if I remove the try/catch I get this error:
                  Unhandled exception type OutOfRangeException
                  • 6. Re: junit - test for exception problem
                    796440
                    You seem to be muddying the difference between
                    exception.OutOfRangeException
                    and
                    OutOfRangeException
                    By the way, I would not advise putting exceptions in their own package.
                    • 7. Re: junit - test for exception problem
                      807605
                      Yes, it does compile.
                      You are catching the exception.
                      junit expects to see the exception itself. You are
                      catching it so it doesn't see it.
                      I understand what you are saying. I originally didn't
                      type in the try/catch in the test method. But if I
                      remove the try/catch I get this error:
                      Unhandled exception type OutOfRangeException
                      I don't know much about Junit 4, but my preferred way of testing exceptions is to surround them in a try/catch as you have but to call Assert.fail() at the end of the try block
                      public void testScoreOutOfRangeException() {
                          try {
                              record.setScore (-1);
                              fail("should have thrown an OutOfRangeException)";
                          }
                          catch ( OutOfRangeException e ) {
                               // do nothing because we expected it
                          }
                      }
                      You'll have to look at Teh Docs for JUnit4 to see how to force a fail, though
                      • 8. Re: junit - test for exception problem
                        807605
                        By the way, I would not advise putting exceptions in their own package.
                        Can you tell me why you discourage this?
                        • 9. Re: junit - test for exception problem
                          796440
                          try {
                          record.setScore (-1);
                          fail("should have thrown an
                          OutOfRangeException)";
                          }
                          Yes. Exactly.
                          • 10. Re: junit - test for exception problem
                            796440
                            By the way, I would not advise putting exceptions
                            in their own package.
                            Can you tell me why you discourage this?
                            A package is for a group of classes that collaborate very closely together. A bunch of exceptions don't collaborate that closely together, to the exclusion of the classes that throw them.
                            • 11. Re: junit - test for exception problem
                              jschellSomeoneStoleMyAlias
                              Yes, it does compile.
                              You are catching the exception.
                              junit expects to see the exception itself. You are
                              catching it so it doesn't see it.
                              I understand what you are saying. I originally didn't
                              type in the try/catch in the test method. But if I
                              remove the try/catch I get this error:
                              Unhandled exception type OutOfRangeException
                              And is the following the actual fully qualified name of the exception?

                              exception.OutOfRangeException.class
                              • 12. Re: junit - test for exception problem
                                807605
                                A package is for a group of classes that collaborate very closely together <snip>
                                I understand your point. Suppose I have packages/classes like so:
                                car
                                  Ford
                                  Toyota
                                  Nissan
                                motorcycle
                                  Yamaha
                                  Harley
                                If all these classes use OutOfRange exception, then it seems misleading to include in one package or the other.

                                How to best handle this?
                                • 13. Re: junit - test for exception problem
                                  jschellSomeoneStoleMyAlias
                                  A package is for a group of classes that
                                  collaborate very closely together <snip>
                                  I understand your point. Suppose I have
                                  packages/classes like so:
                                  car
                                  Ford
                                  Toyota
                                  Nissan
                                  torcycle
                                  Yamaha
                                  Harley
                                  If all these classes use OutOfRange exception, then
                                  it seems misleading to include in one package or the
                                  other.

                                  How to best handle this?
                                  Err...by refactoring. Because nothing in that hierarchy suggests in any way that those classes should exist. At best there should only be two classes: Car and Motorcycle. And even that might be too many.
                                  • 14. Re: junit - test for exception problem
                                    796440
                                    A package is for a group of classes that
                                    collaborate very closely together <snip>
                                    I understand your point. Suppose I have
                                    packages/classes like so:
                                    car
                                    Ford
                                    Toyota
                                    Nissan
                                    torcycle
                                    Yamaha
                                    Harley
                                    If all these classes use OutOfRange exception, then
                                    it seems misleading to include in one package or the
                                    other.

                                    How to best handle this?
                                    We would not have that hierarchy in the first place.

                                    If we did, there would probably be a "parent" package called vehicle or something, and OORE would be defined there, as a subclass of VehicleException.

                                    But then, we really wouldn't have that hierarchy in the first place.
                                    1 2 3 Previous Next