I've been trying to figure out why 2 of my test methods in this class have been failing.
First, here is the Student class:
public class Student
{
// Information about the individual student
private double gpa;
private String firstName, lastName, id;
private int currentEarnedCr; // Current earned credits (already completed)
private int anticipatedAdditionalCr; // Anticipated additional credits (currently taking)
private boolean lascComplete; // Has the student completed LASC requirements
private boolean majorComplete; //Has the student completed requirements for the major
// Minimum number of credits required to graduate
public static final int REQUIRED_CR = 120;
// Keeps track of the id number to assign to the next student to be created
private static int nextId = 1;
/**
* Creates a new student given a first and last name. An id number is assigned sequentially.
*
* @param firstName the student's first name
* @param lastName the student's last name
*/
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = String.format("%07d",nextId); // 7 digits wide, zero padded
nextId++;
}
/**
* Returns the number of students which have been created.
*
* @return the number of students which have been created
*/
public static int getStudentCount() {
return nextId-1;
}
/**
* Set the GPA of the student.
*
* @param gpa the student's GPA
*/
public void setGpa(double gpa) {
this.gpa = gpa;
}
/**
* Returns the student's GPA.
*
* @return the student's GPA
*/
public double getGpa() {
return gpa;
}
/**
* Sets the student's first name.
*
* @param firstName the student's first name
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Returns the student's first name.
*
* @returns the student's first name
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the student's last name.
*
* @param lastName the student's last name
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Returns the student's last name.
*
* @returns the student's last name
*/
public String getLastName() {
return lastName;
}
/**
* Returns the student's ID number.
*
* @returns the student's ID number
*/
public String getId() {
return id;
}
/**
* Sets the student's current earned credits.
* (This should really be done by looking at a
* list of completed courses, not by setting it directly.)
*
* @param currentEarnedCr the number of current earned credits
*/
public void setCurrentEarnedCr(int currentEarnedCr) {
this.currentEarnedCr = currentEarnedCr;
}
/**
* Returns the student's current earned credits.
*
* @return the student's current earned credits
*/
public int getCurrentEarnedCr() {
// This should really be done by looking at a
// list of completed courses, not by returning a variable.
return currentEarnedCr;
}
/**
* Sets the student's anticipated additional credits (the
* courses they are currently taking).
* (This should really be done by looking at a
* list of registered courses, not by setting it directly.)
*
* @param anticipatedAdditionalCr the number of anticipated additional credits
*/
public void setAnticipatedAdditionalCr(int anticipatedAdditionalCr) {
this.anticipatedAdditionalCr = anticipatedAdditionalCr;
}
/**
* Returns the student's anticipated additional credits.
*
* @return the student's anticipated additional credits
*/
public int getAnticipatedAdditionalCr() {
// This should really be done by looking at a
// list of registered courses, not by returning a variable.
return anticipatedAdditionalCr;
}
/**
* Sets whether the student has completed the LASC requirements.
* (This should really be done by looking at a
* list of completed courses, not by setting it directly.)
*
* @param lascComplete whether LASC requirements are complete
*/
public void setLascComplete(boolean lascComplete) {
this.lascComplete = lascComplete;
}
/**
* Returns whether the student has completed the LASC requirements.
*
* @return whether the student has completed the LASC requirements
*/
public boolean getLascComplete() {
// This should really be done by looking at a
// list of complete courses, not by returning a variable.
return lascComplete;
}
/**
* Sets whether the student has completed the major requirements.
* (This should really be done by looking at a
* list of completed courses, not by setting it directly.)
*
* @param majorComplete whether major requirements are complete
*/
public void setMajorComplete(boolean majorComplete) {
this.majorComplete = majorComplete;
}
/**
* Returns whether the student has completed the major requirements.
*
* @return whether the student has completed the major requirements
*/
public boolean getMajorComplete() {
// This should really be done by looking at a
// list of complete courses, not by returning a variable.
return majorComplete;
}
/**
* Returns the student's remaining credits to graduate
* (not including the courses they are currently taking).
*
* @return the student's remaining credits to graduate
*/
public int getCurrentRemainingCr() {
return (currentEarnedCr >= 120) ? 0 : (REQUIRED_CR - currentEarnedCr);
}
/**
* Returns the student's anticipated remaining credits to graduate
* (including the courses they are currently taking).
*
* @return the student's anticipated remaining credits to graduate
*/
public int getAnticipatedRemainingCr() {
return getCurrentRemainingCr() - anticipatedAdditionalCr;
}
/**
* Returns whether the student is ready to graduate.
*
* @return whether the student is ready to graduate
*/
public boolean readyToGraduate() {
return getCurrentRemainingCr() == 0 && gpa >= 2.0 && lascComplete && majorComplete;
}
}
And here is my JUnitTest Class:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class StudentTest{
private Student student1;
@Before
public void setUp() {
student1 = new Student("Jane", "Smith");
}
@Test
public void testGetCurrentEarnedCr() {
int credits = 45;
student1.setCurrentEarnedCr(credits);
assertEquals(credits, student1.getCurrentEarnedCr());
}
@Test
public void testAnticipatedAdditionalCr(){
int credits = 45;
student1.setAnticipatedAdditionalCr(credits);
assertEquals(credits, student1.getAnticipatedAdditionalCr());
}
@Test
public void testReadyToGraduate1(){
student1.setCurrentEarnedCr(120);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertTrue(student1.readyToGraduate());
}
@Test
public void testReadyToGraduate2(){
student1.setCurrentEarnedCr(120);
student1.setGpa(2.0);
student1.setMajorComplete(false);
student1.setLascComplete(true);
assertFalse(student1.readyToGraduate());
}
@Test
public void testReadyToGraduate3(){
student1.setCurrentEarnedCr(120);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(false);
assertFalse(student1.readyToGraduate());
}
@Test
public void testReadyToGraduate4(){
student1.setCurrentEarnedCr(120);
student1.setGpa(1.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertFalse(student1.readyToGraduate());
}
@Test
public void testReadyToGraduate5(){
student1.setCurrentEarnedCr(119);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertFalse(student1.readyToGraduate());
}
@Test
public void testReadyToGraduate6(){
student1.setCurrentEarnedCr(121);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertTrue(student1.readyToGraduate());
}
@Test
public void testGetCurrentRemainingCr1(){
student1.setCurrentEarnedCr(119);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertFalse(student1.readyToGraduate());
}
@Test
public void testGetCurrentRemainingCr2(){
student1.setCurrentEarnedCr(120);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertTrue(student1.readyToGraduate());
}
@Test
public void testGetCurrentRemainingCr3(){
student1.setCurrentEarnedCr(121);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertTrue(student1.readyToGraduate());
}
@Test
public void testStudentCount(){
int count = Student.getStudentCount();
new Student("Joe", "Jones");
assertEquals(count+1,Student.getStudentCount());
}
@Test
public void testStudentID(){
int count = Student.getStudentCount();
Student s2 = new Student("Joe", "Jones");
assertEquals(String.format("%07d", count+1),s2.getId());
}
}
Now for some reason I have 2 methods that are still failing with testing.
@Test
public void testGetCurrentRemainingCr3(){
student1.setCurrentEarnedCr(121);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertTrue(student1.readyToGraduate());
}
and
@Test
public void testReadyToGraduate6(){
student1.setCurrentEarnedCr(121);
student1.setGpa(2.0);
student1.setMajorComplete(true);
student1.setLascComplete(true);
assertTrue(student1.readyToGraduate());
}
Basically, 120 credits is enough to graduate so when it's testing to see if you are ready to graduate the test should pass even if you are over 120 credits. In the student class I fixed the getCurrentRemainingCr method to accomodate this...here is that method below.
/**
* Returns the student's remaining credits to graduate
* (not including the courses they are currently taking).
*
* @return the student's remaining credits to graduate
*/
public int getCurrentRemainingCr() {
return (currentEarnedCr >= 120) ? 0 : (REQUIRED_CR - currentEarnedCr);
}
I do not see any reason why these two test methods are still failing. Any suggestions and help would be very much appreciated.