所以对于我的编程类,我们应该对继承自抽象类的一个类进行 Junit 测试。我只是对编写Junit测试感到非常困惑,因为我觉得网上没有足够的示例。我的第一个问题是,我们是否测试吸盘手和二传手?我的教授指示我们在设置测试时"选择所有"方法,但是在她要求我们阅读的网站上,他们建议不要测试getter和setter,因为JVM已经有测试器了。我的第二个也是最重要的问题是你能测试整个构造函数吗?我下面的代码基本上有一个本科生继承自抽象学生班。我假设我应该进行测试以确保本科生实际上是本科生而不是硕士生,但我不知道如何准确测试整个构造函数。任何提示将不胜感激!!
public abstract class AbstractStudent {
/**
* First name of a student.
*/
private String myFirstName;
/**
* Last name of a student.
*/
private String myLastName;
/**
* Student 9-digit ID.
*/
private String myID;
/**
* Number of credit hours completed by a student.
*/
private int myCreditHours;
/**
* Current student GPA.
*/
private double myGPA;
/**
* Student gender.
*/
private Gender myGender;
/** Student date of birth.
*
*/
private Date myBirth;
/**
* Private constructor to prohibit default instantiation.
*/
/**
* @param theFirstName Adds the first name into the constructor
* @param theLastName Adds the last name into the constructor
* @param theID Adds the ID into the constructor
* @param theCreditHours Adds the credit hours into the constructor
* @param theGPA Adds the GPA into the constructor
* @param theGender Adds the gender into the constructor
* @param theBirth Adds the birth into the constructor
*/
public AbstractStudent(final String theFirstName, final String theLastName,
final String theID, final int theCreditHours, final double theGPA,
final Gender theGender, final Date theBirth) {
myFirstName = theFirstName;
myLastName = theLastName;
myID = theID;
myCreditHours = theCreditHours;
myGPA = theGPA;
myGender = theGender;
myBirth = new Date(theBirth.getTime());
}
本科生班:
public class UndergradStudent extends AbstractStudent {
/**
* Student status.
*/
private StudentStatus myStatus;
/**
* Parameterized constructor - constructors a Student object.
* @param theFirstName is a string representing the first name of a student, != null
* @param theLastName is a string representing the last name of a student, != null
* @param theID is a string of 9 characters representing student ID
* @param theCreditHours is an integer number >=0 representing number of credit hours
* taken by a student
* @param theGPA is a double number representing GPA, a GPA must be >= 0 and <= 4.0
* @param theStatus is a string representing student status, != null
* @param theGender is a character representing student gender, != null
* @param theBirth is a date representing student birth date; a student cannot be younger
* than 10 years old
* @custom.post Student object constructed; if invalid parameters passed,
* student is in an invalid state.
*/
public UndergradStudent(final String theFirstName, final String theLastName,
final String theID, final int theCreditHours, final double theGPA,
final StudentStatus theStatus, final Gender theGender,
final Date theBirth) {
super(theFirstName, theLastName, theID, theCreditHours, theGPA, theGender, theBirth);
myStatus = theStatus;
}
我不能真正评论你教授的意图,但在编写单元测试时,你的目标是测试单个功能单元。 大多数开发人员考虑测试基本的 POJO 行为,其中 get 方法将构造函数属性作为冗余返回,但是如果您正在编写有状态的 getter/setter 方法,则值得测试。
测试UndergradStudent
的整个构造函数的一种方法是将实例构造为私有最终变量或使用 JUnit 的@BeforeClass
注释并为每个属性 getter 编写单独的@Test
。