【问题标题】:How to test ArrayIndexOutOfBoundsException?如何测试 ArrayIndexOutOfBoundsException?
【发布时间】:2021-04-24 09:29:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: java testing junit args arrayindexoutofboundsexception


    【解决方案1】:
    @Test(expected = ArrayIndexOutOfBoundsException.class) 
    public void throwsArrayIndexException() {
      int [] array = new int[] {};
      int num = array[1];
    }
    

    【讨论】:

    • 您好,欢迎来到 SO!虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。请阅读tourHow do I write a good answer?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多