• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TestCase for ArrayIndexOutOfBounds

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you write a testcase to test ArrayIndexOutOfBounds for an array in junit? Thanks in advance.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

VenkataPrasad Regula wrote:How do you write a testcase to test ArrayIndexOutOfBounds for an array in junit? Thanks in advance.


You write out the cases where you expect it to occur, and where you don't, and then write methods to prove it.
eg, Array size=5, index=10, you would probably expect a failure - so write a method that tries to get the 10th element of an array of size 5.

The more "creative" stuff is when you get into 'edge' and 'corner' cases; otherwise I'd probably use calls to a single method with randomly generated sizes and indexes and make sure they behave as I expect (called a 'smoke test').

Winston
 
VenkataPrasad Regula
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to the above question is at the link www.coderanch.com/t/96328/Testing/handle-exception-JUnit-code
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see why you would want to explicitly test for an ArrayIndexOutOfBoundsException as expected behavior.
It's a runtime exception that is most likely indicative of a bug somewhere in the code, and it should be prevented from occuring by validating input.
If input validation fails you could throw an exception, but I wouldn't choose ArrayIndexOutOfBoundsException for that purpose.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jelle Klap wrote:I don't see why you would want to explicitly test for an ArrayIndexOutOfBoundsException as expected behavior...


I totally agree; however, maybe its a QA exercise.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic