Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

junit - test for exception problem

807605Jul 30 2007 — edited Aug 3 2007
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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 31 2007
Added on Jul 30 2007
41 comments
1,983 views