如何测试 ArrayIndexOutOfBoundsException?

问题描述

我从未用 JUnit 测试过,不知道在这种情况下我必须做什么

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

我只需要测试如果在 main 方法中 args[0]==0, Arrayindexoutofboundsexception 是预期的,我尝试了 if(args[0]==0){throw new ArrayIndexOutOfBoundException} 但没有工作.. 我感谢您的帮助!提前致谢!

解决方法

@Test(expected = ArrayIndexOutOfBoundsException.class) 
public void throwsArrayIndexException() {
  int [] array = new int[] {};
  int num = array[1];
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...