gpt4 book ai didi

java - Junit 测试构造器

转载 作者:行者123 更新时间:2023-11-28 21:29:09 25 4
gpt4 key购买 nike

所以对于我的编程课,我们应该为我们的一个从抽象类继承的类进行 Junit 测试。我对编写 Junit 测试感到非常困惑,因为我觉得网上没有足够的示例。我的第一个问题是,我们是否测试 getter 和 setter?我的教授指示我们在设置测试时“选择所有”方法,但在她要求我们阅读的网站上,他们建议不要测试 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;
}

最佳答案

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

测试 UndergradStudent 的整个构造函数的一种方法是将实例构造为私有(private)最终变量或使用 JUnit 的 @BeforeClass注释和编写单独的 @Test对于每个属性 setter/getter 。

关于java - Junit 测试构造器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29594514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com