2015-04-12 142 views
0

因此,对于我的编程类,我们应该为继承自抽象类的其中一个类进行Junit测试。我在编写Junit测试时感到非常困惑,因为我不觉得网上有足够的例子。我的第一个问题是,我们测试getter和setters吗?我的教授指示我们在设置我们的测试时“选择所有”方法,但在她要求我们阅读的网站上,他们建议不要测试getter和setter,因为JVM已经拥有测试人员。我的第二个也是最重要的问题是你可以测试一个完整的构造函数吗?我的下面的代码基本上有一个从抽象的学生类继承的本科生。我假设我应该测试以确保本科学生实际上是本科生,而不是硕士生,但我不知道如何精确测试整个构造函数。任何提示将非常感谢!!Junit测试构造函数

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

在我看来,测试getter和setter并不重要,除非它们包含业务逻辑(这种逻辑不应该驻留在那里)。坚持测试应用程序重要部分的口头禅,而不是使代码覆盖率成为您的目标。 – JamesB

+0

所以我可以创建一个包含整个构造函数的单个测试方法,或者你说我应该测试一些特定的领域例如:名字,姓氏,gpa等等。我只是觉得如果我为每个东西做了一个单独的测试方法,它会占用大量的空间。谢谢! – user3376654

+0

测试方法,而不是字段,不担心你有多少测试,只要确保他们正在测试不同的逻辑块。 – JamesB

回答

1

我不能在你的教授的用意真的发表评论,但编写单元测试时,你的目标是要测试的功能单一的单位。大多数开发人员考虑测试基本的POJO行为,其中get方法将构造函数属性返回为冗余,但是如果您正在编写有状态的getter/setter方法,则值得测试。你可以测试的UndergradStudent整个构造

一种方式是建立一个实例既可以作为私人最终变量或使用JUnit的@BeforeClass注释,写每个属性的getter单独@Test