Junit测试构造器

时间:2015-04-12 20:51:50

标签: java inheritance testing junit constructor

因此,对于我的编程类,我们应该为我们继承自抽象类的一个类进行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());

}

UndergradStudent课程:

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;
}

1 个答案:

答案 0 :(得分:1)

我无法评论您教授的意图,但在编写单元测试时,您的目标是测试单个功能单元。大多数开发人员都会考虑测试基本的POJO行为,其中get方法将构造函数属性作为冗余返回,但是如果您编写有状态的getter / setter方法,则值得测试。

您可以测试UndergradStudent的整个构造函数的一种方法是将实例构造为私有final变量或使用JUnit的@BeforeClass注释并为每个属性getter编写单独的@Test