小妹完全没接触过JUNIT,所以问出白痴问题来大家不要介意。现在是要求根据CLASS的描述生写出TEST CLASS来,大家先看看对下面的代码我理解的对不对class 其中一个方法的描述如下:validateValue
public void validateValue(java.lang.String value)
throws CandidateFieldExceptionChecks that argument value is legal. Used by AbstractCandidateField.setValue(String) to check that given value is valid. Specified by:
validateValue in class AbstractCandidateField
Parameters:
value - Value to be checked
Throws:
CandidateFieldException - If value cannot be casted to int
See Also:
AbstractCandidateField.validateValue(String)那我写的TESTING 如下
public void testValidateValue_WithNull() {
try {
age.validateValue(null);
fail("validateValue method accepts null as argument.");
} catch (CandidateFieldException e) {}
}
那也就是说,如果JUNIT报错并输出"validateValue method accepts null as argument."那就说明原程序接受了NULL值为有效值,是个BUG。如果原程序有排除NULL的话,那TESTING执行到
age.validateValue(null);的时候就直接产生异常了,这样JUNIT反而不报错,对吗?那在测试CONSTRUCTOR的时候,我就不明白了public void testConstructor() throws CandidateFieldException {
CandidateField ageField = new Age("Age", new Properties());
assertNotNull("Contructor returned null.", ageField);
}CandidateField ageField = new Age("Age", new Properties());根据描述是可以生成一个Age型INSTANCE,那应该就是要执行下面那一行喽,当然它也是非空的,那起码应该在JUNIT显示这句话吧?
为什么也JUNIT就认为它是对的,也不报错也不显示括号里的内容呢,看起来好象没执行一样?
public void validateValue(java.lang.String value)
throws CandidateFieldExceptionChecks that argument value is legal. Used by AbstractCandidateField.setValue(String) to check that given value is valid. Specified by:
validateValue in class AbstractCandidateField
Parameters:
value - Value to be checked
Throws:
CandidateFieldException - If value cannot be casted to int
See Also:
AbstractCandidateField.validateValue(String)那我写的TESTING 如下
public void testValidateValue_WithNull() {
try {
age.validateValue(null);
fail("validateValue method accepts null as argument.");
} catch (CandidateFieldException e) {}
}
那也就是说,如果JUNIT报错并输出"validateValue method accepts null as argument."那就说明原程序接受了NULL值为有效值,是个BUG。如果原程序有排除NULL的话,那TESTING执行到
age.validateValue(null);的时候就直接产生异常了,这样JUNIT反而不报错,对吗?那在测试CONSTRUCTOR的时候,我就不明白了public void testConstructor() throws CandidateFieldException {
CandidateField ageField = new Age("Age", new Properties());
assertNotNull("Contructor returned null.", ageField);
}CandidateField ageField = new Age("Age", new Properties());根据描述是可以生成一个Age型INSTANCE,那应该就是要执行下面那一行喽,当然它也是非空的,那起码应该在JUNIT显示这句话吧?
为什么也JUNIT就认为它是对的,也不报错也不显示括号里的内容呢,看起来好象没执行一样?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货