0

I have never tested with JUnit, not sure what I have to do in this case

@Test(expected = ArrayIndexOutOfBoundsException.class)
public void throwsArrayIndexException() {
    ...
}

I just have to test that if args[0]==0 in main method, ArrayIndexOutOfBoundsException is expected, I tried with if(args[0]==0){throw new ArrayIndexOutOfBoundException} but didn't work.. I appreciate your help! Thanks in advance!

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72

1 Answers1

0
@Test(expected = ArrayIndexOutOfBoundsException.class) 
public void throwsArrayIndexException() {
  int [] array = new int[] {};
  int num = array[1];
}
키믄수
  • 1
  • 1
  • 2
    Hello and welcome to SO! While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Mhd Alaa Alhaj Jan 21 '21 at 08:25