【问题标题】:My JUnit test case for a try - catch constructor isnt working?我的 try-catch 构造函数的 JUnit 测试用例不起作用?
【发布时间】:2022-11-03 07:26:46
【问题描述】:

我试图将一个字符串传递给我的构造函数,看看它是否会抛出一个 NumberFormatException。

> public Student(String datenZeile) ...

String[] teile = datenZeile.split(",");
                
  name = teile[0];
  matrikelnummer = Integer.parseInt(teile[1]);
  studiengang = teile[2];
...


catch (NumberFormatException e) {
            throw e;
        }
This is my JUnit test, but i dont know what to put a the lambda expression.

类学生测试{

@Test
void testGreta() {
    Student greta = new Student("Greta Graf,7-00-06,Medieninformatik,312");
    assertThrows(ArrayIndexOutOfBoundsException.class, () -> 
}

}

【问题讨论】:

    标签: java junit


    【解决方案1】:

    由于它是抛出异常的构造函数,因此您需要在 assertThrows 的 lambda 内部调用它:

    @Test
    void testGreta() {
      assertThrows(ArrayIndexOutOfBoundsException.class,
                   () -> new Student("Greta Graf,7-00-06,Medieninformatik,312")
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-09
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多