【问题标题】:assertEquals doesn't show errors when the test is expecting exceptions当测试预期异常时,assertEquals 不显示错误
【发布时间】:2018-11-17 16:23:09
【问题描述】:

我最近开始与 Junit 合作。

我有一个函数,它接收一行包含从 txt 文件中获取的值,并返回具有该值的实例。

每当该函数接收到错误的值时,我都会抛出异常。

我的测试方法正在验证异常是否正常工作,以及实例返回的值是否正确。

@Test(expected = StudentException.class)
public void testImportStudent() {
    String txtline = "1, Name, Name"; //Wrong, It should be 1, Name
    Source test = studentMain.importStudent(txtline); //Throw error
    txtline = "1, Name"; //Right
    test = percursoGestor.importSource(line); //Test passed
    assertEquals("Error inserting ID", 3, test.getStudentID()); //Test ignores this line
    assertEquals("Error inserting Name", "Nothing", test.getStudentName()); //Test ignores this line
}

因此,我的测试检查是否抛出了错误,但忽略了 assertequals,即使我输入的值与预期不同,测试也通过了。因为我期望抛出异常。

我做错了什么?

【问题讨论】:

    标签: java junit junit4


    【解决方案1】:

    您已使用expected = StudentException.class 注释您的测试方法。

    这基本上是在整个方法周围放置一个try-catch 块:

    @Test(expected = StudentException.class)
    public void testImportStudent() {
        try {
            String txtline = "1, Name, Name";
            Source test = studentMain.importStudent(txtline); //Throw error
            txtline = "1, Name";
            test = percursoGestor.importSource(line);
            assertEquals("Error inserting ID", 3, test.getStudentID());
            assertEquals("Error inserting Name", "Nothing", test.getStudentName());
        } catch (Exception e) {
            // verify exception
        }
    }
    

    和往常一样:抛出异常后不执行任何操作。

    干净的方法是使用两种不同的方法:

    @Test(expected = StudentException.class)
    public void testImportStudent_incorrectInput() {
        String txtline = "1, Name, Name";
        Source test = studentMain.importStudent(txtline);
    }
    
    @Test
    public void testImportStudent_correctInput() {
        String txtline = "1, Name";
        Source test = percursoGestor.importSource(line);
        assertEquals("Error inserting ID", 3, test.getStudentID());
        assertEquals("Error inserting Name", "Nothing", test.getStudentName());    
    }
    

    如果您真的想用一种方法测试多个案例(您可能不这样做),那么您可以自己使用try-catch

    @Test
    public void testImportStudent() {
        String txtline = "1, Name, Name";
        Source test;
        try {
            test = studentMain.importStudent(txtline);
            // Make sure that the test fails when no exception is thrown
            fail();
        } catch (Exception e) {
            // Check for the correct exception type
            assertTrue(e instanceof StudentException);
        }
        // Other checks
        txtline = "1, Name";
        test = percursoGestor.importSource(line);
        assertEquals("Error inserting ID", 3, test.getStudentID());
        assertEquals("Error inserting Name", "Nothing", test.getStudentName());
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      相关资源
      最近更新 更多