Java assertEquals不';当测试预期出现异常时,不显示错误
我最近开始与Junit合作 我有一个函数,它接收一行来自txt文件的值,并返回带有该值的实例 每当该函数接收到错误的值时,我抛出一个异常 我的测试方法是验证异常是否正常工作,以及实例返回的值是否正确Java assertEquals不';当测试预期出现异常时,不显示错误,java,junit,junit4,Java,Junit,Junit4,我最近开始与Junit合作 我有一个函数,它接收一行来自txt文件的值,并返回带有该值的实例 每当该函数接收到错误的值时,我抛出一个异常 我的测试方法是验证异常是否正常工作,以及实例返回的值是否正确 @Test(expected = StudentException.class) public void testImportStudent() { String txtline = "1, Name, Name"; //Wrong, It should be 1, Name Sou
@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,即使我输入了一个与预期不同的值,测试还是通过了。因为我希望抛出一个异常
我做错了什么?您已经用
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());
}
您已经用
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());
}